Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
feat: support scoped generators
Browse files Browse the repository at this point in the history
Add full support for scoped generators including both standard:
```
$ npm init yo @dizmo/generator-dizmo
```
and shorthand generator invocations:
```
$ npm init yo @dizmo/dizmo
```
  • Loading branch information
vladimyr authored and boneskull committed May 12, 2020
1 parent 6a2ae27 commit 2b60c7f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
46 changes: 29 additions & 17 deletions src/create-yo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const npx = importFrom(path.join(globalDirs.npm.packages, 'npm'), 'libnpx');
const symbols = require('log-symbols');
const bold = require('ansi-bold');

const PREFIX = 'generator-';

/**
* Invokes `npx` with `--package yo --package generator-generatorName -- yo generatorName`
*
Expand All @@ -31,21 +29,14 @@ async function create(
);
}

const generator = argv.pop();
let generatorPackage;
let generatorName;
if (generator.startsWith('@')) {
// handle scoped packages
generatorName = generatorPackage = generator;
} else {
generatorPackage = generator.startsWith(PREFIX)
? generator
: `${PREFIX}${generator}`;
generatorName = generatorPackage.slice(PREFIX.length);
}

// handle subgenerators
generatorPackage = generatorPackage.split(':').shift();
const [generator, subgenerator] = argv.pop().split(':');
const generatorPackage = packageName(generator);
const generatorName = [
generatorPackage.replace(/generator-/, ''),
subgenerator
]
.filter(Boolean)
.join(':');

return npx(
npx.parseArgs(
Expand Down Expand Up @@ -77,3 +68,24 @@ async function create(
})();

exports.create = create;

/**
* Get package name for given generator.
*
* @private
* @param {string} generator
* @returns {string} package name
*
* @example
* ```js
* generatorName(`license`) // => `generator-license`
* generatorName(`@dizmo/dizmo`) // => `@dizmo/generator-dizmo`
* ```
*/
function packageName(generator) {
if (/^@[^/]+\//.test(generator)) {
return generator.replace(/^@([^/]+)\/(generator-)?/, '@$1/generator-');
}

return generator.replace(/^(generator-)?/, 'generator-');
}
10 changes: 4 additions & 6 deletions test/create-yo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('create-yo', function() {
await create([
'/path/to/node',
'/path/to/create-yo',
'@scoped-package/generator-some-generator:subgenerator'
'@scoped-package/some-generator:subgenerator'
]);
expect(libnpx, 'to have a call satisfying', {
args: [
Expand All @@ -106,9 +106,7 @@ describe('create-yo', function() {
'@scoped-package/generator-some-generator@latest'
],
command: 'yo',
cmdOpts: [
'@scoped-package/generator-some-generator:subgenerator'
],
cmdOpts: ['@scoped-package/some-generator:subgenerator'],
npm: '/path/to/global/binaries/npm'
}
]
Expand All @@ -120,7 +118,7 @@ describe('create-yo', function() {
await create([
'/path/to/node',
'/path/to/create-yo',
'@scoped-package/generator-some-generator'
'@scoped-package/some-generator'
]);
expect(libnpx, 'to have a call satisfying', {
args: [
Expand All @@ -130,7 +128,7 @@ describe('create-yo', function() {
'@scoped-package/generator-some-generator@latest'
],
command: 'yo',
cmdOpts: ['@scoped-package/generator-some-generator'],
cmdOpts: ['@scoped-package/some-generator'],
npm: '/path/to/global/binaries/npm'
}
]
Expand Down

0 comments on commit 2b60c7f

Please sign in to comment.