Skip to content

Commit

Permalink
Merge pull request #119 from alibaba/dev
Browse files Browse the repository at this point in the history
0.9.4,支持在一个 npm package 中包含多个间件
  • Loading branch information
Houfeng authored Jan 18, 2018
2 parents 7ca94ff + 406a500 commit 87580e3
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 88 deletions.
3 changes: 3 additions & 0 deletions .dawn/pipe.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
lint:
- name: lint

test:
- name: lint
- name: unit
Expand Down
81 changes: 81 additions & 0 deletions lib/common/pkgname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright (c) 2016-present Alibaba Group Holding Limited
* @author Houfeng <[email protected]>
*/

class PkgName {

constructor(opts) {
this.opts = Object.assign({}, opts);
}

get scope() {
return this.opts.scope || '';
}

get version() {
return this.opts.version || '';
}

get prefix() {
return this.opts.prefix || '';
}

get member() {
return this.opts.member || '';
}

get name() {
const { name = '', prefix } = this.opts;
return prefix && !name.startsWith(prefix) ? `${prefix}-${name}` : name;
}

get fullName() {
const { scope, name } = this;
return scope ? `${scope}/${name}` : name;
}

get fullNameAndVersion() {
const { fullName, version } = this;
return version ? `${fullName}@${version}` : fullName;
}

get isPath() {
return !!this.opts.isPath;
}

toString() {
const { fullNameAndVersion, member } = this;
return menubar ? `${fullNameAndVersion} ${member}` : fullNameAndVersion;
}

}

const SCOPE_REGEXP = /^(@\S+?)\//i;
const VERSION_REGEXP = /\S+?@(\S+)\s*/i;
const MEMBER_REGEXP = /\s(\S+)$/i;

function isPath(str) {
if (!str) return false;
return str.startsWith('.') ||
str.startsWith('/') ||
/^[a-z]+\:/i.test(str);
}

function parse(str, prefix) {
str = str || '';
if (isPath(str)) {
const member = (MEMBER_REGEXP.exec(str) || [])[1];
const name = str.replace(MEMBER_REGEXP, '');
return new PkgName({ name, member, isPath: true });
} else {
const scope = (SCOPE_REGEXP.exec(str) || [])[1];
const version = (VERSION_REGEXP.exec(str) || [])[1];
const member = (MEMBER_REGEXP.exec(str) || [])[1];
const name = str.replace(SCOPE_REGEXP, '')
.split('@')[0].split(' ')[0];
return new PkgName({ scope, version, member, name, prefix });
}
}

module.exports = parse;
36 changes: 21 additions & 15 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const path = require('path');
const mod = require('./mod');
const debug = require('debug')('middleware');
const fs = require('fs');
const pkgname = require('./common/pkgname');

exports.list = async function () {
let middlewares = [];
Expand Down Expand Up @@ -52,21 +53,26 @@ exports.getDocUrl = async function (name) {
exports.require = async function (name, cwd) {
cwd = cwd || process.cwd();
debug('require', name, cwd);
if (mod.isFolder(name)) {
return require(path.resolve(cwd, name));
}
let remoteConf = await this.get(name);
if (remoteConf && remoteConf.location) {
name = remoteConf.location;
}
let prefix = await configs.getRc('middlewarePrefix');
let trimedName = mod.parseName(name, prefix).fullName;
let packagePath = path.normalize(`${cwd}/node_modules/${trimedName}`);
if (!fs.existsSync(packagePath)) {
await mod.install(name, {
flag: { 'no-save': true },
prefix: prefix
});
let nameInfo = pkgname(name, prefix);
let mdModule;
if (nameInfo.isPath) {
mdModule = require(path.resolve(cwd, nameInfo.name));
} else {
let remoteConf = await this.get(nameInfo.name);
if (remoteConf && remoteConf.location) {
nameInfo.opts.name = remoteConf.location;
}
let packagePath = path
.normalize(`${cwd}/node_modules/${nameInfo.fullName}`);
if (!fs.existsSync(packagePath)) {
await mod.install(nameInfo.fullNameAndVersion, {
flag: { 'no-save': true },
prefix: prefix
});
}
mdModule = require(packagePath);
}
return require(packagePath);
return mdModule && nameInfo.member ?
mdModule[nameInfo.member] : mdModule;
};
81 changes: 13 additions & 68 deletions lib/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,12 @@ const save = require('./common/save');
const configs = require('./configs');
const trim = require('./common/trim');
const console = require('./common/console');
const pkgname = require('./common/pkgname');

const CNPM_BIN = path.resolve(__dirname, '../node_modules/.bin/cnpm');
const FETCT_TIMEOUT = 30000;
const OFFICIAL_NPM_PKG_URL_PREFIX = 'https://www.npmjs.com/package';

class PackageName {

constructor(name, prefix) {
name = name || '';
if (name && prefix && !name.startsWith(prefix)) {
this.name = `${prefix}-${name}`;
} else {
this.name = name;
}
}

get fullName() {
let name = this.name || '';
if (name && this.scope) {
return `${this.scope}/${name}`;
} else {
return name;
}
}

get fullNameAndVersion() {
if (this.version) {
return `${this.fullName}@${this.version}`;
} else {
return this.fullName;
}
}

}

exports.parseName = function (name, prefix) {
if (!name) return '';
if (name.includes('/')) {
let parts = name.split('/');
return Object.assign(
this.parseName(parts[1], prefix),
{ scope: parts[0] }
);
} else if (name.includes('@')) {
let parts = name.split('@');
return Object.assign(
this.parseName(parts[0], prefix),
{ version: parts[1] }
);
} else {
return new PackageName(name, prefix);
}
};

exports.isFolder = function (name) {
if (!name) return false;
return name.startsWith('.') ||
name.startsWith('/') ||
/^[a-z]+\:/.test(name);
};

exports.exec = async function (cmd, opts) {
opts = Object.assign({}, opts);
opts.cwd = opts.cwd || process.cwd();
Expand All @@ -104,7 +49,7 @@ exports.install = async function (name, opts) {
return;
}
console.info(`Installing '${name || 'dependencies'}' ...`);
let nameInfo = this.parseName(name, opts.prefix);
let nameInfo = pkgname(name, opts.prefix);
delete opts.prefix;
await this.exec(`i ${nameInfo.fullNameAndVersion || ''}`, opts);
console.info('Done');
Expand Down Expand Up @@ -140,23 +85,23 @@ exports.getVersionInfo = async function (name, version) {
exports.download = async function (name, prefix) {
if (!name) return;
console.info(`Downloading '${name}' ...`);
let trimedName = this.parseName(name, prefix).fullName;
debug('download', name, trimedName);
let stamp = new Stamp(`${trimedName}.module`);
let nameInfo = pkgname(name, prefix);
debug('download', name, nameInfo.fullName);
let stamp = new Stamp(`${nameInfo.fullName}.module`);
let storeDir = await store.getPath('modules');
let filename = path.normalize(
`${storeDir}/${trimedName.replace(/\//, '-')}.tgz`
`${storeDir}/${nameInfo.fullName.replace(/\//, '-')}.tgz`
);
let isExists = fs.existsSync(filename);
if (!isExists || await stamp.isExpire()) {
let info = await this.getVersionInfo(trimedName);
if (!info || !info.dist || !info.dist.tarball) {
throw new Error(`Cannot download ${trimedName}`);
let verInfo = await this.getVersionInfo(nameInfo.fullName);
if (!verInfo || !verInfo.dist || !verInfo.dist.tarball) {
throw new Error(`Cannot download ${nameInfo.fullName}`);
}
debug('download url', info.dist.tarball);
debug('download url', verInfo.dist.tarball);
let res;
try {
res = await download(info.dist.tarball);
res = await download(verInfo.dist.tarball);
} catch (err) {
console.info('Done');
if (isExists) return filename;
Expand All @@ -173,10 +118,10 @@ exports.download = async function (name, prefix) {
};

exports.getDocUrl = async function (name, prefix) {
let trimedName = this.parseName(name, prefix).fullName;
let nameInfo = pkgname(name, prefix);
let registryUri = trim(await configs.getRc('registry'), '/');
debug('registryUri', registryUri);
let url = `${OFFICIAL_NPM_PKG_URL_PREFIX}/${trimedName}`;
let url = `${OFFICIAL_NPM_PKG_URL_PREFIX}/${nameInfo.fullName}`;
debug('docUrl', url);
return url;
};
Expand Down
9 changes: 6 additions & 3 deletions lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const fs = require('fs');
const copydir = require('./common/copydir');
const path = require('path');
const debug = require('debug')('template');
const pkgname = require('./common/pkgname');

exports.list = async function () {
let templates = [];
Expand Down Expand Up @@ -85,13 +86,15 @@ exports.getDocUrl = async function (name) {
exports.init = async function (name, cwd) {
cwd = cwd || process.cwd();
debug('init', name, cwd);
let prefix = await configs.getRc('templatePrefix');
let nameInfo = pkgname(name, prefix);
let result;
try {
if (mod.isFolder(name)) {
let src = path.resolve(cwd, name);
if (nameInfo.isPath) {
let src = path.resolve(cwd, nameInfo.name);
result = await copydir(src, cwd);
} else {
result = await this.download(name, cwd);
result = await this.download(nameInfo.fullNameAndVersion, cwd);
}
} catch (err) {
throw err;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dawn",
"version": "0.9.3",
"version": "0.9.4",
"description": "dawn cli",
"main": "./lib/index.js",
"bin": {
Expand Down
2 changes: 1 addition & 1 deletion test/demo1/.dawn/pipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test1:
- name: ./mw1.js

test2:
- name: dn-middleware-unit-demo
- name: dn-middleware-unit-demo@latest
error: mw2
- name: mw2
location: ./mw2.js

0 comments on commit 87580e3

Please sign in to comment.