From af5af6a84c762ee02a0fe0e9565100b296d6aa1f Mon Sep 17 00:00:00 2001 From: Haoliang Gao Date: Fri, 3 Mar 2017 23:00:22 +0800 Subject: [PATCH] feat: update pkg.files that if file exists (#37) Avoid missing files when publish to npm --- .autod.conf | 1 + README.md | 9 ++++++ README.zh-CN.md | 9 ++++++ lib/helper.js | 4 +-- lib/pkgfiles_command.js | 21 +++++++++++++ lib/program.js | 1 + lib/test_command.js | 2 +- package.json | 1 + test/egg-pkgfiles.test.js | 31 +++++++++++++++++++ .../enzyme-example-mocha/package.json | 5 +-- test/fixtures/pkgfiles/app.js | 3 ++ test/fixtures/pkgfiles/app/extend/context.js | 3 ++ .../pkgfiles/config/config.default.js | 3 ++ test/fixtures/pkgfiles/package.json | 1 + test/fixtures/pkgfiles/test/index.test.js | 0 test/fixtures/test-files/package.json | 7 +++-- 16 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 lib/pkgfiles_command.js create mode 100644 test/egg-pkgfiles.test.js create mode 100644 test/fixtures/pkgfiles/app.js create mode 100644 test/fixtures/pkgfiles/app/extend/context.js create mode 100644 test/fixtures/pkgfiles/config/config.default.js create mode 100644 test/fixtures/pkgfiles/package.json create mode 100644 test/fixtures/pkgfiles/test/index.test.js diff --git a/.autod.conf b/.autod.conf index 9d26534f..d0953b0a 100644 --- a/.autod.conf +++ b/.autod.conf @@ -13,6 +13,7 @@ module.exports = { 'co-mocha', 'intelli-espower-loader', 'power-assert', + 'ypkgfiles', ], devdep: [ 'autod', diff --git a/README.md b/README.md index 1aa1edca..a2c9f1e4 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,14 @@ You can set `COV_EXCLUDES` env to add dir ignore coverage. $ COV_EXCLUDES="app/plugins/c*,app/autocreate/**" egg-bin cov ``` +### pkgfiles + +Generate `pkg.files` automatically before npm publish, see [ypkgfiles] for detail + +```bash +$ egg-bin pkgfiles +``` + ### auto require `test/.setup.js` If `test/.setup.js` file exists, it will be auto require on `test` and `cov` command. @@ -221,3 +229,4 @@ run nsp check at /foo/bar with [] [iron-node]: https://github.com/s-a/iron-node [intelli-espower-loader]: https://github.com/power-assert-js/intelli-espower-loader [power-assert]: https://github.com/power-assert-js/power-assert +[ypkgfiles]: https://github.com/popomore/ypkgfiles diff --git a/README.zh-CN.md b/README.zh-CN.md index f0cd6a91..7fea2551 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -115,6 +115,14 @@ TEST_TIMEOUT=2000 egg-bin test $ COV_EXCLUDES="app/plugins/c*,app/autocreate/**" egg-bin cov ``` +### pkgfiles + +在 npm publish 之前使用 pkgfiles 自动生成 `pkg.files`, 更多查看 [ypkgfiles] + +```bash +$ egg-bin pkgfiles +``` + ## 定制属于你团队的 egg-bin 如果你的团队已经基于 egg 开发了属于自己的框架,那么很可能你会需要在 egg-bin 上做更多自定义功能。 @@ -195,3 +203,4 @@ run nsp check at /foo/bar with [] [nsp]: https://npmjs.com/nsp [intelli-espower-loader]: https://github.com/power-assert-js/intelli-espower-loader [power-assert]: https://github.com/power-assert-js/power-assert +[ypkgfiles]: https://github.com/popomore/ypkgfiles diff --git a/lib/helper.js b/lib/helper.js index 10aea7e3..cece3bca 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -1,6 +1,6 @@ 'use strict'; -const fs = require('fs'); +const existsSync = require('fs').existsSync; const path = require('path'); const glob = require('glob'); const detect = require('detect-port'); @@ -23,7 +23,7 @@ exports.getTestFiles = () => { exports.getTestSetupFile = () => { const setupFile = path.join(process.cwd(), 'test/.setup.js'); - if (fs.existsSync(setupFile)) { + if (existsSync(setupFile)) { return setupFile; } return null; diff --git a/lib/pkgfiles_command.js b/lib/pkgfiles_command.js new file mode 100644 index 00000000..fe5e11b1 --- /dev/null +++ b/lib/pkgfiles_command.js @@ -0,0 +1,21 @@ +'use strict'; + +const Command = require('./command'); + +class PkgfilesCommand extends Command { + * run(cwd) { + const args = [ + '--entry', 'app', + '--entry', 'config', + '--entry', '*.js', + ]; + const pkgfiles = require.resolve('ypkgfiles/bin/pkgfiles.js'); + yield this.helper.forkNode(pkgfiles, args, { cwd }); + } + + help() { + return 'Generate pkg.files automatically'; + } +} + +module.exports = PkgfilesCommand; diff --git a/lib/program.js b/lib/program.js index 4e3b9104..cbdcaefa 100644 --- a/lib/program.js +++ b/lib/program.js @@ -15,6 +15,7 @@ class Program extends BaseProgram { } else { this.addCommand('cov', path.join(__dirname, 'cov_command.js')); } + this.addCommand('pkgfiles', path.join(__dirname, 'pkgfiles_command.js')); } } diff --git a/lib/test_command.js b/lib/test_command.js index b0b74d41..3c83cab6 100644 --- a/lib/test_command.js +++ b/lib/test_command.js @@ -3,7 +3,7 @@ const Command = require('./command'); class TestCommand extends Command { - * run(_, args) { + * run(cwd, args) { yield this.helper.checkDeps(); const newArgs = this.helper.formatTestArgs(args); diff --git a/package.json b/package.json index cbb9a30c..7d95d09e 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "mocha": "^3.2.0", "power-assert": "^1.4.2", "rimraf": "^2.6.1", + "ypkgfiles": "^1.1.0", "yargs": "^6.6.0" }, "devDependencies": { diff --git a/test/egg-pkgfiles.test.js b/test/egg-pkgfiles.test.js new file mode 100644 index 00000000..746d1fc2 --- /dev/null +++ b/test/egg-pkgfiles.test.js @@ -0,0 +1,31 @@ +'use strict'; + +const path = require('path'); +const assert = require('assert'); +const coffee = require('coffee'); +const fs = require('mz/fs'); + +describe('egg-bin pkgfiles', () => { + const eggBin = require.resolve('../bin/egg-bin.js'); + + let cwd; + afterEach(() => fs.writeFile(path.join(cwd, 'package.json'), '{}')); + + it('should update pkg.files', function* () { + cwd = path.join(__dirname, 'fixtures/pkgfiles'); + yield fs.writeFile(path.join(cwd, 'package.json'), '{}'); + + yield coffee.fork(eggBin, [ 'pkgfiles' ], { cwd }) + // .debug() + .expect('code', 0) + .end(); + + const body = yield fs.readFile(path.join(cwd, 'package.json'), 'utf8'); + assert.deepEqual(JSON.parse(body).files, [ + 'app', + 'config', + 'app.js', + ]); + }); + +}); diff --git a/test/fixtures/enzyme-example-mocha/package.json b/test/fixtures/enzyme-example-mocha/package.json index 7fccd08e..abbe24d4 100644 --- a/test/fixtures/enzyme-example-mocha/package.json +++ b/test/fixtures/enzyme-example-mocha/package.json @@ -27,5 +27,6 @@ "dependencies": { "react": "^0.14.7", "react-dom": "^0.14.7" - } -} + }, + "files": [] +} \ No newline at end of file diff --git a/test/fixtures/pkgfiles/app.js b/test/fixtures/pkgfiles/app.js new file mode 100644 index 00000000..529aa95a --- /dev/null +++ b/test/fixtures/pkgfiles/app.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = () => {}; diff --git a/test/fixtures/pkgfiles/app/extend/context.js b/test/fixtures/pkgfiles/app/extend/context.js new file mode 100644 index 00000000..8b46fbba --- /dev/null +++ b/test/fixtures/pkgfiles/app/extend/context.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = {}; diff --git a/test/fixtures/pkgfiles/config/config.default.js b/test/fixtures/pkgfiles/config/config.default.js new file mode 100644 index 00000000..8b46fbba --- /dev/null +++ b/test/fixtures/pkgfiles/config/config.default.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = {}; diff --git a/test/fixtures/pkgfiles/package.json b/test/fixtures/pkgfiles/package.json new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/test/fixtures/pkgfiles/package.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/pkgfiles/test/index.test.js b/test/fixtures/pkgfiles/test/index.test.js new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/test-files/package.json b/test/fixtures/test-files/package.json index 37cbfb28..ee78010c 100644 --- a/test/fixtures/test-files/package.json +++ b/test/fixtures/test-files/package.json @@ -1,3 +1,6 @@ { - "name": "test-files" -} + "name": "test-files", + "files": [ + "lib" + ] +} \ No newline at end of file