From 406a5008a01b842b16fe47c93a3b7f77011dee8e Mon Sep 17 00:00:00 2001 From: Houfeng Date: Fri, 19 Jan 2018 01:50:59 +0800 Subject: [PATCH] =?UTF-8?q?0.9.4=EF=BC=8C=E6=94=AF=E6=8C=81=E5=9C=A8?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=20npm=20package=20=E4=B8=AD=E5=8C=85?= =?UTF-8?q?=E5=90=AB=E5=A4=9A=E4=B8=AA=E9=97=B4=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dawn/pipe.yml | 3 ++ lib/common/pkgname.js | 81 +++++++++++++++++++++++++++++++++++++++ lib/middleware.js | 36 +++++++++-------- lib/mod.js | 81 +++++++-------------------------------- lib/template.js | 9 +++-- package.json | 2 +- test/demo1/.dawn/pipe.yml | 2 +- 7 files changed, 126 insertions(+), 88 deletions(-) create mode 100644 lib/common/pkgname.js diff --git a/.dawn/pipe.yml b/.dawn/pipe.yml index f9b61bd2..a93c433e 100644 --- a/.dawn/pipe.yml +++ b/.dawn/pipe.yml @@ -1,3 +1,6 @@ +lint: + - name: lint + test: - name: lint - name: unit diff --git a/lib/common/pkgname.js b/lib/common/pkgname.js new file mode 100644 index 00000000..b4063176 --- /dev/null +++ b/lib/common/pkgname.js @@ -0,0 +1,81 @@ +/** + * Copyright (c) 2016-present Alibaba Group Holding Limited + * @author Houfeng + */ + +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; \ No newline at end of file diff --git a/lib/middleware.js b/lib/middleware.js index f25c032a..a610ec62 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -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 = []; @@ -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; }; \ No newline at end of file diff --git a/lib/mod.js b/lib/mod.js index 255eae02..3ce04886 100644 --- a/lib/mod.js +++ b/lib/mod.js @@ -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(); @@ -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'); @@ -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; @@ -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; }; diff --git a/lib/template.js b/lib/template.js index 35e2c9f9..73efdd96 100644 --- a/lib/template.js +++ b/lib/template.js @@ -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 = []; @@ -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; diff --git a/package.json b/package.json index a8453c7a..65ebf3a0 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dawn", - "version": "0.9.3", + "version": "0.9.4", "description": "dawn cli", "main": "./lib/index.js", "bin": { diff --git a/test/demo1/.dawn/pipe.yml b/test/demo1/.dawn/pipe.yml index 18ec339d..2d07f5e0 100644 --- a/test/demo1/.dawn/pipe.yml +++ b/test/demo1/.dawn/pipe.yml @@ -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