Skip to content

Commit baf1557

Browse files
committed
chore: update init.ts
1 parent 6248ee6 commit baf1557

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"make:docs": "typedoc",
2424
"test": "jest --coverage",
2525
"test:ci": "cross-env CI=1 jest",
26-
"prepare": "yarn run build",
2726
"postinstall": "ts-node tools/init"
2827

2928
},

tools/init.ts

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const { mv, rm, which, exec } = require('shelljs');
2323
// Note: These should all be relative to the project root directory
2424
const rmDirs = ['.git', 'tools'];
2525
const rmFiles = ['.all-contributorsrc', '.gitattributes'];
26-
const modifyFiles = ['LICENSE', 'package.json', 'example/package.json'];
26+
const modifyFiles = ['LICENSE', 'package.json'];
2727
const renameFiles: Array<string> = [];
2828

2929
/**
@@ -60,7 +60,7 @@ function modifyContents(
6060

6161
const files = modifyFiles.map(f => resolve(__dirname, '..', f));
6262
try {
63-
replace({
63+
replace.sync({
6464
files,
6565
from: [
6666
/--libraryname--/g,
@@ -74,13 +74,12 @@ function modifyContents(
7474
} catch (error) {
7575
console.error('An error occurred modifying the file: ', error);
7676
}
77-
7877
console.log('\n');
7978
}
8079

8180
function modifyGitignoreFile(): void {
8281
const files = ['.gitignore'].map(f => resolve(__dirname, '..', f));
83-
replace({
82+
replace.sync({
8483
files,
8584
from: ['dist/', 'docs/'],
8685
to: '',
@@ -109,29 +108,6 @@ function renameItems(libraryName: string) {
109108
console.log('\n');
110109
}
111110

112-
const _promptInstallExampleApp = {
113-
properties: {
114-
installExampleApp: {
115-
description: cyan(
116-
'Would you like to generate an example react app to test your library/component?'
117-
),
118-
pattern: /^(y(es)?|n(o)?)$/i,
119-
type: 'string',
120-
required: true,
121-
message: 'You need to type "Yes" or "No" to continue...',
122-
},
123-
},
124-
};
125-
function installExampleApp() {
126-
_prompt.get(_promptInstallExampleApp, (err: any, res: any) => {
127-
if (err) return;
128-
if (res.installExampleApp.toLowerCase().charAt(0) === 'y') {
129-
exec('npx create-react-app example');
130-
exec('echo "SKIP_PREFLIGHT_CHECK=true" >> example/.env');
131-
}
132-
});
133-
}
134-
135111
/**
136112
* Calls any external programs to finish setting up the library
137113
*/
@@ -162,14 +138,16 @@ function finalize() {
162138
console.log(yellow('Removing yarn.lock and performing a clean install...'));
163139
rm('yarn.lock');
164140
exec('yarn install');
141+
exec('yarn build');
142+
exec("git add . && git commit -am 'chore: init' --no-verify");
165143
console.log('\n');
166144
}
167145
/**
168146
* Calls all of the functions needed to setup the library
169147
*
170148
* @param libraryName
171149
*/
172-
function setupLibrary(libraryName: string) {
150+
function setupLibrary(libraryName: string, generateExample = false) {
173151
console.log(
174152
cyan(
175153
'\nThanks for the info. The last few changes are being made... hang tight!\n\n'
@@ -180,13 +158,24 @@ function setupLibrary(libraryName: string) {
180158
const username = exec('git config user.name').stdout.trim();
181159
const usermail = exec('git config user.email').stdout.trim();
182160

161+
if (generateExample) {
162+
yellow('Installing the test application into example/ directory...');
163+
exec('npx create-react-app example');
164+
exec('echo "SKIP_PREFLIGHT_CHECK=true" >> example/.env');
165+
}
183166
removeItems();
184167

185168
modifyGitignoreFile();
186-
installExampleApp();
187169
modifyContents(libraryName, username, usermail);
188170
// renameItems(libraryName);
189171

172+
if (generateExample) {
173+
console.log(yellow('Linking packages to the example app...'));
174+
exec(
175+
'cd example && yarn add link:.. link:../node_modules/react link:../node_modules/react-dom && cd ..'
176+
);
177+
}
178+
190179
finalize();
191180

192181
console.log(cyan("OK, you're all set. Happy coding!! ;)\n"));
@@ -237,6 +226,21 @@ const _promptSchemaLibrarySuggest = {
237226
},
238227
},
239228
};
229+
230+
const _promptInstallExampleApp = {
231+
properties: {
232+
installExampleApp: {
233+
description: yellow(
234+
'Would you like to generate an example react app to test your library/component? [Yes/No]'
235+
),
236+
pattern: /^(y(es)?|n(o)?)$/i,
237+
type: 'string',
238+
required: true,
239+
message: 'You need to type "Yes" or "No" to continue...',
240+
},
241+
},
242+
};
243+
240244
/**
241245
* Asks the user for the name of the library if it has been cloned into the
242246
* default directory, or if they want a different name to the one suggested
@@ -249,8 +253,14 @@ function libraryNameCreate() {
249253
process.exit(1);
250254
return;
251255
}
252-
253-
setupLibrary(res.library);
256+
_prompt.get(_promptInstallExampleApp, (err: any, res: any) => {
257+
let installExampleApp = false;
258+
if (err) return;
259+
if (res.installExampleApp.toLowerCase().charAt(0) === 'y') {
260+
installExampleApp = true;
261+
}
262+
setupLibrary(res.library, installExampleApp);
263+
});
254264
});
255265
}
256266

@@ -266,7 +276,14 @@ function libraryNameSuggestedAccept() {
266276
}
267277

268278
if (res.useSuggestedName.toLowerCase().charAt(0) === 'y') {
269-
setupLibrary(libraryNameSuggested());
279+
_prompt.get(_promptInstallExampleApp, (err: any, res: any) => {
280+
let installExampleApp = false;
281+
if (err) return;
282+
if (res.installExampleApp.toLowerCase().charAt(0) === 'y') {
283+
installExampleApp = true;
284+
}
285+
setupLibrary(libraryNameSuggested(), installExampleApp);
286+
});
270287
} else {
271288
libraryNameCreate();
272289
}

0 commit comments

Comments
 (0)