-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd-angular-to-qwik.ts
More file actions
153 lines (134 loc) · 4.32 KB
/
Copy pathadd-angular-to-qwik.ts
File metadata and controls
153 lines (134 loc) · 4.32 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* eslint-disable @typescript-eslint/no-var-requires */
import * as yargs from 'yargs';
import * as chalk from 'chalk';
import { CreateWorkspaceOptions } from 'create-nx-workspace/';
import {
getPackageManagerCommand,
detectPackageManager,
} from 'nx/src/utils/package-manager';
import { output } from 'create-nx-workspace/src/utils/output';
import { readFileSync, writeFileSync, rmSync } from 'fs';
import { execSync } from 'child_process';
interface Arguments extends CreateWorkspaceOptions {
installMaterialExample: boolean;
}
export const commandsObject: yargs.Argv<Arguments> = yargs
.wrap(yargs.terminalWidth())
.parserConfiguration({
'strip-dashed': true,
'dot-notation': true,
})
.command<Arguments>(
// this is the default and only command
'$0 [options]',
'Add Angular to the Qwik workspace',
(yargs) => [
yargs.option('installMaterialExample', {
describe: chalk.dim`Add dependencies for the Angular Material and qwikified example component, that uses it`,
type: 'boolean',
}),
],
async (argv: yargs.ArgumentsCamelCase<Arguments>) => {
await main(argv).catch((error) => {
const { version } = require('../package.json');
output.error({
title: `Something went wrong! v${version}`,
});
throw error;
});
}
)
.help('help', chalk.dim`Show help`)
.version(
'version',
chalk.dim`Show version`,
require('../package.json').version
) as yargs.Argv<Arguments>;
async function main(parsedArgs: yargs.Arguments<Arguments>) {
let isQwikNxInstalled = false;
let isNxDevkitInstalled = false;
const pm = getRelevantPackageManagerCommand();
output.log({
title: `Adding Angular to your workspace.`,
bodyLines: [
'To make sure the command works reliably in all environments, and that the integration is applied correctly,',
`We will run "${pm.install}" several times. Please wait.`,
],
});
try {
// letting Nx think that's an Nx repo
writeFileSync(
'project.json',
JSON.stringify({
name: 'temp-project',
sourceRoot: 'src',
projectType: 'application',
targets: {},
})
);
isQwikNxInstalled = checkIfPackageInstalled('qwik-nx');
if (!isQwikNxInstalled) {
execSync(`${pm.add} qwik-nx@latest nx@latest`, { stdio: [0, 1, 2] });
}
isNxDevkitInstalled = checkIfPackageInstalled('@nx/devkit');
if (!isNxDevkitInstalled) {
execSync(`${pm.addDev} @nx/devkit@latest`, { stdio: [0, 1, 2] });
}
const installMaterialExample = parsedArgs['installMaterialExample'];
const installMaterialExampleFlag =
installMaterialExample === true || installMaterialExample === false
? `--installMaterialExample=${parsedArgs['installMaterialExample']}`
: undefined;
const cmd = [
'npx nx g qwik-nx:angular-in-app',
'--project=temp-project',
installMaterialExampleFlag,
'--skipFormat',
].filter(Boolean);
execSync(cmd.join(' '), { stdio: [0, 1, 2] });
} catch (error) {
output.error({
title: 'Failed to add angular to your repo',
bodyLines: ['Reverting changes.', 'See original printed error above.'],
});
cleanup(isQwikNxInstalled, pm.uninstall);
process.exit(1);
}
cleanup(isQwikNxInstalled, pm.uninstall);
output.log({
title: `Successfully added Angular integration to your repo`,
});
}
function checkIfPackageInstalled(pkg: string): boolean {
const packageJson = JSON.parse(readFileSync('package.json', 'utf-8'));
return (
!!packageJson['dependencies']?.[pkg] ||
!!packageJson['devDependencies']?.[pkg]
);
}
function getRelevantPackageManagerCommand() {
const pm = detectPackageManager();
const pmc = getPackageManagerCommand(pm);
let uninstall: string;
if (pm === 'npm') {
uninstall = 'npm uninstall';
} else if (pm === 'yarn') {
uninstall = 'yarn remove';
} else {
uninstall = 'pnpm remove';
}
return {
install: pmc.install,
add: pmc.add,
addDev: pmc.addDev,
uninstall,
};
}
function cleanup(isQwikNxInstalled: boolean, uninstallCmd: string) {
rmSync('.nx', { force: true, recursive: true });
rmSync('project.json');
if (!isQwikNxInstalled) {
// TODO: remove deps from package.json and simply run npm install
execSync(`${uninstallCmd} qwik-nx nx`, { stdio: [0, 1, 2] });
}
}