UNPKG

2.22 kBPlain TextView Raw
1import * as fs from 'fs';
2import * as path from 'path';
3import * as shell from 'shelljs';
4import * as updateNotifier from 'update-notifier';
5
6const silent = true;
7const nutmegDir = path.resolve(__dirname, '..');
8const pkg = loadPackageJson(nutmegDir);
9
10interface Pkg {
11 dependencies: { [index: string]: string };
12 main: string;
13 name: string;
14 version: string;
15}
16
17function tsconfigPath(workingDir: string): string {
18 return path.resolve(workingDir, 'tsconfig.production.json');
19}
20
21function notifyOfUpdate() {
22 updateNotifier({ pkg }).notify({ defer: true });
23}
24
25function 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
34function loadPackageJson(dir: string): Pkg {
35 const packagePath = path.resolve(dir, 'package.json');
36 return JSON.parse(fs.readFileSync(packagePath).toString());
37}
38
39
40function exit(message: string, condition = true): void {
41 if (condition) {
42 console.error(message);
43 process.exit(1);
44 }
45}
46
47function 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
54function 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/** Copied from @nutmeg/seed. Make changes there. */
64export function attributeNameFromProperty(name :string): string {
65 return name.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase();
66}
67
68/** Copied from @nutmeg/seed. Make changes there. */
69export 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
77export { commitToGit, exit, installDependencies, isNutmegComponent, loadPackageJson, notifyOfUpdate, tsconfigPath, nutmegDir, pkg };