Skip to content

Commit dc58e3d

Browse files
committed
Fix broken file picker for new library
The template wasn't waiting for `yarn add {libraryName}` to complete, so it didn't find any files to select
1 parent 1811613 commit dc58e3d

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

_new/index.js

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const chalk = require('chalk');
66
const yosay = require('yosay');
77
const inquirer = require('inquirer');
88
const packageJson = require('package-json');
9+
const spawn = require('cross-spawn');
10+
const eos = require('end-of-stream');
911
const globby = require('globby');
1012
const { formatVersionFolder } = require('../utility');
1113

@@ -18,15 +20,22 @@ module.exports = class extends Generator {
1820
)
1921
);
2022

23+
let yarnAdd;
24+
2125
const prompts = [
2226
{
2327
type: 'input',
2428
name: 'libraryName',
2529
message: "What is the npm module's name?",
2630
validate: (libraryName, answers) => {
27-
this.spawnCommand('yarn', ['add', libraryName], {
28-
stdio: 'ignore',
29-
});
31+
yarnAdd = new Promise((resolve, reject) =>
32+
eos(
33+
spawn('yarn', ['add', libraryName], {
34+
stdio: 'ignore',
35+
}),
36+
err => (err ? reject(err) : resolve())
37+
)
38+
);
3039

3140
return packageJson(libraryName, {
3241
fullMetadata: true,
@@ -86,32 +95,36 @@ module.exports = class extends Generator {
8695
type: 'list',
8796
name: 'relativePath',
8897
message: 'What is the main JavaScript file?',
89-
choices: answers => {
90-
try {
91-
return globby
92-
.sync(`node_modules/${answers.libraryName}/**/*.js`)
93-
.map(file =>
94-
file.replace(
95-
`node_modules/${answers.libraryName}/`,
96-
''
98+
choices: ({ libraryName }) =>
99+
yarnAdd
100+
.then(() =>
101+
globby([
102+
`node_modules/${libraryName}/**/*.js`,
103+
`!node_modules/${libraryName}/node_modules/**/*.js`,
104+
])
105+
)
106+
.then(files =>
107+
files
108+
.map(file =>
109+
file.replace(
110+
`node_modules/${libraryName}/`,
111+
''
112+
)
97113
)
98-
)
99-
.map(path.normalize)
100-
.map(file => file.replace(/\\/g, '/'))
101-
.concat([new inquirer.Separator(), 'Other']);
102-
} catch (err) {
103-
this.log.error(
104-
'There was an unexpected error retrieving files: \n %O',
105-
err
106-
);
114+
.map(path.normalize)
115+
.map(file => file.replace(/\\/g, '/'))
116+
.concat([new inquirer.Separator(), 'Other'])
117+
)
118+
.catch(err => {
119+
this.log.error(
120+
'There was an unexpected error retrieving files: \n %O',
121+
err
122+
);
107123

108-
return ['Other'];
109-
}
110-
},
111-
default: answers =>
112-
path
113-
.normalize(answers.pkg.browser || answers.pkg.main)
114-
.replace(/\\/g, '/'),
124+
return ['Other'];
125+
}),
126+
default: ({ pkg }) =>
127+
path.normalize(pkg.browser || pkg.main).replace(/\\/g, '/'),
115128
filter: relativePath => relativePath.replace(/\\/g, '/'),
116129
},
117130
{

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"chalk": "^2.4.2",
8484
"cliui": "5.0.0",
8585
"cross-spawn": "6.0.5",
86+
"end-of-stream": "1.4.1",
8687
"fancy-log": "1.3.3",
8788
"glob": "^7.1.3",
8889
"globby": "^10.0.1",

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,7 @@ encodeurl@^1.0.2:
14311431
resolved "https://registry.yarnpkg.com/encodeurl/-/encodeurl-1.0.2.tgz#ad3ff4c86ec2d029322f5a02c3a9a606c95b3f59"
14321432
integrity sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=
14331433

1434-
end-of-stream@^1.0.0, end-of-stream@^1.1.0:
1434+
end-of-stream@1.4.1, end-of-stream@^1.0.0, end-of-stream@^1.1.0:
14351435
version "1.4.1"
14361436
resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.4.1.tgz#ed29634d19baba463b6ce6b80a37213eab71ec43"
14371437
integrity sha512-1MkrZNvWTKCaigbn+W15elq2BB/L22nqrSY5DKlo3X6+vclJm8Bb5djXJBmEX6fS3+zCh/F4VBK5Z2KxJt4s2Q==

0 commit comments

Comments
 (0)