Skip to content

Commit

Permalink
feat: support scoped package name, fix #236
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLLLLH committed Mar 6, 2021
1 parent b28421d commit 375916b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
34 changes: 34 additions & 0 deletions __tests__/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,38 @@ describe('generator:app', () => {
assert.fileContent('.eslintignore', '**/templates\n');
});
});

describe('scoped name', () => {
beforeEach(() => {
return helpers.run(path.join(__dirname, '../app')).withPrompts({
name: '@yeoman/generator-temp',
description: 'A node generator',
homepage: 'http://yeoman.io',
githubAccount: 'yeoman',
authorName: 'The Yeoman Team',
authorEmail: '[email protected]',
authorUrl: 'http://yeoman.io',
keywords: [],
license: 'MIT'
});
});

it('created and CD into a folder named like the generator', () => {
assert.equal(path.basename(process.cwd()), 'generator-temp');
});

it('fills package.json with correct information', () => {
// eslint-disable-next-line new-cap
assert.JSONFileContent('package.json', {
name: '@yeoman/generator-temp'
});
});

it('fills the README with project data', () => {
assert.fileContent('README.md', '# @yeoman/generator-temp');
assert.fileContent('README.md', 'npm install -g yo');
assert.fileContent('README.md', 'npm install -g @yeoman/generator-temp');
assert.fileContent('README.md', 'yo @yeoman/temp');
});
});
});
28 changes: 23 additions & 5 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@ const _ = require('lodash');
const extend = require('deep-extend');
const mkdirp = require('mkdirp');

function parseScopedName(name) {
const nameFragments = name.split('/');
const parseResult = {
scopeName: '',
localName: name
};

if (nameFragments.length > 1) {
parseResult.scopeName = nameFragments[0];
parseResult.localName = nameFragments[1];
}

return parseResult;
}

function makeGeneratorName(name) {
const parsedName = parseScopedName(name);
name = parsedName.localName;
name = _.kebabCase(name);
name = name.indexOf('generator-') === 0 ? name : 'generator-' + name;
return name;
return parsedName.scopeName ? `${parsedName.scopeName}/${name}` : name;
}

module.exports = class extends Generator {
Expand All @@ -31,16 +48,17 @@ module.exports = class extends Generator {
this
).then(props => {
this.props.name = props.name;
Object.assign(this.props, parseScopedName(props.name));
});
}

default() {
if (path.basename(this.destinationPath()) !== this.props.name) {
if (path.basename(this.destinationPath()) !== this.props.localName) {
this.log(
`Your generator must be inside a folder named ${this.props.name}\nI'll automatically create this folder.`
`Your generator must be inside a folder named ${this.props.localName}\nI'll automatically create this folder.`
);
mkdirp.sync(this.props.name);
this.destinationRoot(this.destinationPath(this.props.name));
mkdirp.sync(this.props.localName);
this.destinationRoot(this.destinationPath(this.props.localName));
}

const readmeTpl = _.template(this.fs.read(this.templatePath('README.md')));
Expand Down

0 comments on commit 375916b

Please sign in to comment.