Skip to content

Commit

Permalink
fix: mem fs bugs in windows (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes committed Feb 21, 2019
1 parent 0704648 commit 318fc69
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 27 deletions.
24 changes: 24 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
@@ -0,0 +1,24 @@
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [ ] `npm test` passes
- [ ] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [ ] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -10,3 +10,5 @@ run
.idea
.DS_Store
.tmp
.vscode
yarn.lock
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,9 +1,9 @@
sudo: false
language: node_js
node_js:
- '4'
- '6'
- '8'
- '10'
install:
- npm i npminstall && npminstall
script:
Expand Down
1 change: 0 additions & 1 deletion appveyor.yml
@@ -1,6 +1,5 @@
environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'
- nodejs_version: '8'

Expand Down
29 changes: 12 additions & 17 deletions lib/init_command.js
Expand Up @@ -9,9 +9,7 @@ const updater = require('npm-updater');
const mkdirp = require('mkdirp');
const inquirer = require('inquirer');
const yargs = require('yargs');
const memFs = require('mem-fs');
const editor = require('mem-fs-editor');
const glob = require('glob');
const glob = require('globby');
const is = require('is-type-of');
const homedir = require('node-homedir');
const compressing = require('compressing');
Expand Down Expand Up @@ -243,24 +241,21 @@ module.exports = class Command {
* processFiles(targetDir, templateDir) {
const src = path.join(templateDir, 'boilerplate');
const locals = yield this.askForVariable(targetDir, templateDir);
const fsEditor = editor.create(memFs.create());
const files = glob.sync('**/*', { cwd: src, dot: true, nodir: true });
const files = glob.sync('**/*', { cwd: src, dot: true });
files.forEach(file => {
const from = path.join(src, file);
const to = path.join(targetDir, this.replaceTemplate(this.fileMapping[file] || file, locals));
fsEditor.copy(from, to, {
process: (content, filePath) => {
this.log('write to %s', to);
if (isTextOrBinary.isTextSync(filePath, content)) { // check if filepath's content is a text file
return this.replaceTemplate(content, locals);
}
return content;
},
});
});
const content = fs.readFileSync(from, { encoding: 'utf-8' });
this.log('write to %s', to);

// write file to disk
yield new Promise(resolve => fsEditor.commit(resolve));
// check if content is a text file
const result = isTextOrBinary.isTextSync(from, content)
? this.replaceTemplate(content, locals)
: content;

mkdirp.sync(path.dirname(to));
fs.writeFileSync(to, result);
});
return files;
}

Expand Down
14 changes: 6 additions & 8 deletions package.json
Expand Up @@ -20,28 +20,26 @@
],
"dependencies": {
"co": "^4.6.0",
"colors": "^1.3.2",
"compressing": "^1.3.1",
"colors": "^1.3.3",
"compressing": "^1.4.0",
"egg-init-config": "^1.5.0",
"glob": "^7.1.3",
"globby": "^9.0.0",
"inquirer": "^3.3.0",
"is-type-of": "^1.2.1",
"istextorbinary": "^2.3.0",
"mem-fs": "^1.1.3",
"mem-fs-editor": "^4.0.3",
"istextorbinary": "^2.5.1",
"mkdirp": "^0.5.1",
"mz-modules": "^2.1.0",
"node-homedir": "^1.1.1",
"npm-updater": "^3.0.3",
"proxy-agent": "^2.3.1",
"urllib": "^2.31.2",
"urllib": "^2.33.0",
"yargs": "^11.1.0"
},
"devDependencies": {
"autod": "^3.0.1",
"coffee": "^3.3.0",
"egg-bin": "^1.11.1",
"egg-ci": "^1.10.0",
"egg-ci": "^1.11.0",
"eslint": "^4.19.1",
"eslint-config-egg": "^6.0.0",
"mm": "^2.4.1",
Expand Down
11 changes: 11 additions & 0 deletions test/init.test.js
Expand Up @@ -74,4 +74,15 @@ describe('test/init.test.js', () => {
assert(command.replaceTemplate('hi, {{ user }}', {}) === 'hi, {{ user }}');
assert(command.replaceTemplate('hi, \\{{ user }}', { user: 'egg' }) === 'hi, {{ user }}');
});

it('should works with remote boilerplate', function* () {
yield command.run(tmp, [ 'simple-app', '--type=simple', '--silent' ]);

assert(fs.existsSync(path.join(command.targetDir, '.gitignore')));
assert(fs.existsSync(path.join(command.targetDir, '.eslintrc')));
assert(fs.existsSync(path.join(command.targetDir, 'package.json')));
assert(fs.existsSync(path.join(command.targetDir, 'package.json')));
assert(fs.existsSync(path.join(command.targetDir, 'test/app/controller/home.test.js')));
assert(fs.existsSync(path.join(command.targetDir, 'app/controller/home.js')));
});
});

0 comments on commit 318fc69

Please sign in to comment.