Skip to content

Commit

Permalink
[feature] support through directory initialization project & support …
Browse files Browse the repository at this point in the history
…for specifying middleware directories through name
  • Loading branch information
Houfeng committed Sep 27, 2017
1 parent 5d966a9 commit 39174d5
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 17 deletions.
15 changes: 15 additions & 0 deletions lib/common/copydir.js
@@ -0,0 +1,15 @@
/**
* Copyright (c) 2017 Alibaba Group Holding Limited
* @author Houfeng <admin@xhou.net>
*/

const copydir = require('copy-dir');

module.exports = function (src, dst) {
return new Promise((resolve, reject) => {
copydir(src, dst, function (err) {
if (err) return reject(err);
return resolve(dst);
});
});
};
2 changes: 2 additions & 0 deletions lib/common/utils.js
Expand Up @@ -24,6 +24,7 @@ const globby = require('globby');
const confman = require('confman');
const stream2buffer = require('./stream2buffer');
const buffer2stream = require('./buffer2stream');
const copydir = require('./copydir');

utils.exec = exec;
utils.writeFile = writeFile;
Expand Down Expand Up @@ -51,5 +52,6 @@ utils.streamToBuffer = stream2buffer;
utils.bufferToStream = buffer2stream;
utils.stream2buffer = stream2buffer;
utils.buffer2stream = buffer2stream;
utils.copydir = copydir;

module.exports = utils;
5 changes: 3 additions & 2 deletions lib/context.js
Expand Up @@ -68,6 +68,7 @@ class Context extends EventEmitter {
async _mergeRemotePipeline(list) {
if (!(await this.configIsExists())) return [];
let pipe = (await configs.getRemoteConf('pipe')) || {};
list = list || [];
pipe.before = pipe.before || [];
pipe.after = pipe.after || [];
let beforeList = pipe.before[this.command] || [];
Expand All @@ -84,7 +85,7 @@ class Context extends EventEmitter {
}

async _loadPipeline() {
if (!(await this.configIsExists())) return;
if (!(await this.configIsExists())) return [];
let list = await this._loadLocalePipeline();
return this._mergeRemotePipeline(list);
}
Expand Down Expand Up @@ -134,7 +135,7 @@ class Context extends EventEmitter {
if (opts.location) {
modFactory = require(path.resolve(this.cwd, opts.location));
} else {
modFactory = await middleware.require(opts.name);
modFactory = await middleware.require(opts.name, this.cwd);
}
if (!utils.isFunction(modFactory)) {
throw new Error(`Invalid middleware '${opts.name}'`);
Expand Down
9 changes: 6 additions & 3 deletions lib/middleware.js
Expand Up @@ -44,13 +44,16 @@ exports.get = async function (name) {
return list.find(item => item.name === name);
};

exports.require = async function (name) {
debug('require', 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 cwd = process.cwd();
let prefix = await configs.getRc('middlewarePrefix');
let trimedName = mod.parseName(name, prefix).fullName;
let packagePath = path.normalize(`${cwd}/node_modules/${trimedName}`);
Expand Down
2 changes: 1 addition & 1 deletion lib/middlewares/init.js
Expand Up @@ -38,7 +38,7 @@ module.exports = async function (next) {
return this.console.warn('No template found');
}
this.console.info('Init template...');
await template.extract(templateName, this.cwd);
await template.init(templateName, this.cwd);
this.console.info('Done');
return next();
};
7 changes: 7 additions & 0 deletions lib/mod.js
Expand Up @@ -69,6 +69,13 @@ exports.parseName = function (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 Down
25 changes: 19 additions & 6 deletions lib/template.js
Expand Up @@ -10,6 +10,8 @@ const mod = require('./mod');
const globby = require('globby');
const rename = require('./common/rename');
const fs = require('fs');
const copydir = require('./common/copydir');
const path = require('path');
const debug = require('debug')('template');

exports.list = async function () {
Expand Down Expand Up @@ -61,8 +63,8 @@ exports.renameFiles = async function (target, suffix) {
}));
};

exports.extract = async function (name, target) {
debug('extract begin', name, target);
exports.download = async function (name, target) {
debug('download', name, target);
let remoteConf = await this.get(name);
debug('remoteConf', name, remoteConf);
if (remoteConf && remoteConf.location) {
Expand All @@ -71,14 +73,25 @@ exports.extract = async function (name, target) {
let prefix = await configs.getRc('templatePrefix');
let filename = await mod.download(name, prefix);
if (!filename) throw new Error('Download error:', name);
debug('extract done', filename, target);
debug('download done', filename, target);
return extract(filename, target, 1);
};

exports.init = async function (name, cwd) {
cwd = cwd || process.cwd();
debug('init', name, cwd);
let result;
try {
result = await extract(filename, target, 1);
if (mod.isFolder(name)) {
let src = path.resolve(cwd, name);
result = await copydir(src, cwd);
} else {
result = await this.download(name, cwd);
}
} catch (err) {
throw err;
}
await this.renameFiles(this.cwd, 'rename');
await this.renameFiles(this.cwd, 'template');
await this.renameFiles(cwd, 'rename');
await this.renameFiles(cwd, 'template');
return result;
};
5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "dawn",
"version": "0.7.5",
"version": "0.7.6",
"description": "dawn cli",
"main": "./lib/index.js",
"bin": {
Expand Down Expand Up @@ -57,6 +57,7 @@
"cnpm": "^5.1.1",
"confman": "^0.2.3",
"console3": "^1.0.5",
"copy-dir": "^0.3.0",
"debug": "^2.6.8",
"decompress": "^4.2.0",
"decompress-targz": "^4.1.1",
Expand All @@ -82,4 +83,4 @@
"nyc": "^11.1.0",
"pre-push": "^0.1.1"
}
}
}
5 changes: 4 additions & 1 deletion test/demo1/.dawn/pipe.yml
@@ -1,7 +1,10 @@
test1:
- name: dn-middleware-unit-demo
value: mw1
- name: ./mw1.js

test2:
- name: dn-middleware-unit-demo
error: mw2
error: mw2
- name: mw2
location: ./mw2.js
6 changes: 6 additions & 0 deletions test/demo1/mw1.js
@@ -0,0 +1,6 @@
module.exports = function () {
return async function (next) {
this.console.log('mw1.js');
next();
};
};
6 changes: 6 additions & 0 deletions test/demo1/mw2.js
@@ -0,0 +1,6 @@
module.exports = function () {
return async function (next) {
this.console.log('mw2.js');
next();
};
};
13 changes: 11 additions & 2 deletions test/template.test.js
Expand Up @@ -19,11 +19,20 @@ describe('template', function () {
expect(result2.length < 1).to.be.equal(true);
});

it('#extract', async function () {
it('#init by package', async function () {
let tmpDir = process.env.TMP || process.env.TMPDIR;
let target = `${tmpDir}/${utils.newGuid()}`;
await mkdirp(target);
await template.extract('dn-template-unit-demo', target);
await template.init('dn-template-unit-demo', target);
let isDone = fs.existsSync(`${target}/package.json`);
expect(isDone).to.be.equal(true);
});

it('#init by path', async function () {
let tmpDir = process.env.TMP || process.env.TMPDIR;
let target = `${tmpDir}/${utils.newGuid()}`;
await mkdirp(target);
await template.init(path.resolve(__dirname, './demo1'), target);
let isDone = fs.existsSync(`${target}/package.json`);
expect(isDone).to.be.equal(true);
});
Expand Down

0 comments on commit 39174d5

Please sign in to comment.