-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathafter.js
More file actions
134 lines (108 loc) · 3.82 KB
/
after.js
File metadata and controls
134 lines (108 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Use "after" task to ask user to install deps.
const {execSync} = require('child_process');
const fs = require('fs');
const path = require('path');
const PNPM_NPMRC = `# for pnpm, use flat node_modules
shamefully-hoist=true
auto-install-peers=true
`;
function isAvailable(bin) {
try {
execSync(bin + ' -v', {stdio: 'ignore'});
return true;
} catch (e) {
return false;
}
}
module.exports = async function({
unattended, here, prompts, run, properties, features, notDefaultFeatures, ansiColors
}, {
// for testing
_isAvailable = isAvailable,
_log = console.log
} = {}) {
const c = ansiColors;
let depsInstalled = false;
let packageManager = undefined;
const projectDir = here ? '.' : properties.name;
if (!unattended) {
const choices = [ {value: 'npm', title: 'Yes, use npm'} ];
if (_isAvailable('yarn')) {
choices.push({value: 'yarn', title: 'Yes, use yarn (node-modules)'});
}
if (_isAvailable('pnpm')) {
choices.push({value: 'pnpm', title: 'Yes, use pnpm'});
}
choices.push({title: 'No'});
packageManager = await prompts.select({
message: 'Do you want to install npm dependencies now?',
choices
});
if (packageManager) {
if (packageManager === 'pnpm') {
ensurePnpmrc(projectDir);
}
await run(packageManager, ['install']);
if (features.includes('playwright')) {
if (packageManager === 'npm') {
await run('npx', ['playwright', 'install', '--with-deps']);
} else {
await run(packageManager, ['dlx', 'playwright', 'install', '--with-deps']);
}
}
depsInstalled = true;
}
_log(`\nNext time, you can try to create similar project in silent mode:`);
_log(c.inverse(` npx makes aurelia new-project-name${here ? ' --here' : ''} -s ${notDefaultFeatures.length ? (notDefaultFeatures.join(',') + ' ') : ''}`));
}
// Setup Storybook directory and files
if (features.includes('storybook')) {
try {
// Navigate to project directory if we're not in it already
const projectDir = here ? '.' : properties.name;
const originalCwd = process.cwd();
if (!here && fs.existsSync(projectDir)) {
process.chdir(projectDir);
}
// Create .storybook directory
if (!fs.existsSync('.storybook')) {
fs.mkdirSync('.storybook');
}
// Move and rename storybook configuration files
const extension = features.includes('typescript') ? '.ts' : '.js';
const mainFile = `storybook-main${extension}`;
const previewFile = `storybook-preview${extension}`;
if (fs.existsSync(mainFile)) {
fs.renameSync(mainFile, `.storybook/main${extension}`);
}
if (fs.existsSync(previewFile)) {
fs.renameSync(previewFile, `.storybook/preview${extension}`);
}
// Return to original directory
if (!here && originalCwd !== process.cwd()) {
process.chdir(originalCwd);
}
} catch (error) {
_log(c.yellow(`Warning: Could not setup .storybook directory: ${error.message}`));
}
}
_log(`\n${c.underline.bold('Get Started')}`);
if (!here) _log('cd ' + properties.name);
if (!depsInstalled) {
_log('npm install');
if (features.includes('playwright')) _log('npx playwright install --with-deps');
}
_log((packageManager ?? 'npm') + ' start\n');
};
function ensurePnpmrc(projectDir) {
if (!projectDir || !fs.existsSync(projectDir)) return;
const npmrcPath = path.join(projectDir, '.npmrc');
if (!fs.existsSync(npmrcPath)) {
fs.writeFileSync(npmrcPath, PNPM_NPMRC);
return;
}
const existing = fs.readFileSync(npmrcPath, 'utf8');
if (existing.includes('shamefully-hoist=') && existing.includes('auto-install-peers=')) return;
const spacer = existing.endsWith('\n') ? '' : '\n';
fs.writeFileSync(npmrcPath, existing + spacer + PNPM_NPMRC);
}