1 | import * as fs from 'fs';
|
2 | import * as path from 'path';
|
3 | import * as shell from 'shelljs';
|
4 | import * as updateNotifier from 'update-notifier';
|
5 |
|
6 | const silent = true;
|
7 | const nutmegDir = path.resolve(__dirname, '..');
|
8 | const pkg = loadPackageJson(nutmegDir);
|
9 |
|
10 | interface Pkg {
|
11 | dependencies: { [index: string]: string };
|
12 | main: string;
|
13 | name: string;
|
14 | version: string;
|
15 | }
|
16 |
|
17 | function tsconfigPath(workingDir: string): string {
|
18 | return path.resolve(workingDir, 'tsconfig.production.json');
|
19 | }
|
20 |
|
21 | function notifyOfUpdate() {
|
22 | updateNotifier({ pkg }).notify({ defer: true });
|
23 | }
|
24 |
|
25 | function isNutmegComponent(workingDir: string): boolean {
|
26 | try {
|
27 | const { dependencies } = loadPackageJson(workingDir);
|
28 | return dependencies && dependencies.hasOwnProperty('@nutmeg/seed');
|
29 | } catch(e) {
|
30 | return false;
|
31 | }
|
32 | }
|
33 |
|
34 | function loadPackageJson(dir: string): Pkg {
|
35 | const packagePath = path.resolve(dir, 'package.json');
|
36 | return JSON.parse(fs.readFileSync(packagePath).toString());
|
37 | }
|
38 |
|
39 |
|
40 | function exit(message: string, condition = true): void {
|
41 | if (condition) {
|
42 | console.error(message);
|
43 | process.exit(1);
|
44 | }
|
45 | }
|
46 |
|
47 | function commitToGit(): void {
|
48 | shell.exec('git init', { silent });
|
49 | shell.exec('git add .', { silent });
|
50 | shell.exec('git commit -m "Initial commit from @nutmeg/cli"', { silent });
|
51 | console.log('🗄️ Commiting files to initial Git repository');
|
52 | }
|
53 |
|
54 | function installDependencies(options: { withDependencies: boolean }): void {
|
55 | if (!options.withDependencies) {
|
56 | console.log('📦 Skipping dependencies');
|
57 | } else {
|
58 | console.log(`🎁 Installing dependencies`);
|
59 | shell.exec('npm install', { silent });
|
60 | }
|
61 | }
|
62 |
|
63 |
|
64 | export function attributeNameFromProperty(name :string): string {
|
65 | return name.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase();
|
66 | }
|
67 |
|
68 |
|
69 | export function propertyNameFromAttribute(name :string): string {
|
70 | if (name.includes('-')) {
|
71 | return name.toLowerCase().replace(/-([a-z])/g, (g) => g[1].toUpperCase());
|
72 | } else {
|
73 | return name;
|
74 | }
|
75 | }
|
76 |
|
77 | export { commitToGit, exit, installDependencies, isNutmegComponent, loadPackageJson, notifyOfUpdate, tsconfigPath, nutmegDir, pkg };
|