|
1 | 1 | import {readFileSync, writeFileSync, mkdirSync} from 'fs'; |
2 | 2 | import {join} from 'path'; |
| 3 | +import {sortPackageJson} from 'sort-package-json' |
3 | 4 | import {fileExists} from '../tools/file'; |
4 | 5 |
|
5 | 6 | function createFile(pathToFile: string): void { |
@@ -33,25 +34,40 @@ const files = [ |
33 | 34 | join('tutorial', '02', '02.spec.js') |
34 | 35 | ]; |
35 | 36 |
|
36 | | -export function createTutorialMd(): void { |
| 37 | +export function createTutorialMd(): Promise<boolean> { |
37 | 38 | return new Promise((resolve, reject) => { |
38 | 39 | folders.forEach((folder) => createFolder(folder)); |
39 | 40 | files.forEach((file) => createFile(file)); |
40 | | - resolve(); |
| 41 | + resolve(true); |
41 | 42 | }); |
42 | 43 | } |
43 | 44 |
|
44 | | -export function createPackageJson(name: string): void { |
| 45 | +export function createPackageJson(name: string): Promise<boolean> { |
45 | 46 | return new Promise((resolve, reject) => { |
46 | 47 | if (!fileExists('package.json')) { |
47 | | - let inputPath: string = join(__dirname, '..', '..', 'setup', 'package.json'); |
48 | | - let packageJson: PackageJson = JSON.parse(readFileSync(inputPath, 'utf8')); |
| 48 | + |
| 49 | + // read from existing package.json |
| 50 | + const inputPath: string = join( |
| 51 | + __dirname, '..', '..', 'setup', 'package.json' |
| 52 | + ); |
| 53 | + |
| 54 | + const packageJson: PackageJson = JSON.parse( |
| 55 | + readFileSync(inputPath, 'utf8') |
| 56 | + ); |
49 | 57 | packageJson.name = 'coderoad-' + name; |
50 | | - let packageJsonString: string = JSON.stringify(packageJson, null, 2); |
| 58 | + |
| 59 | + // sort package.json keys |
| 60 | + const packageJsonString: string = sortPackageJson( |
| 61 | + JSON.stringify(packageJson, null, 2) |
| 62 | + ); |
| 63 | + |
51 | 64 | writeFileSync('package.json', packageJsonString, 'utf8'); |
52 | | - resolve(); |
| 65 | + resolve(true); |
53 | 66 | } else { |
54 | | - resolve(); |
| 67 | + // TODO: validate package.json |
| 68 | + |
| 69 | + // already created |
| 70 | + resolve(true); |
55 | 71 | } |
56 | 72 | }); |
57 | 73 | } |
0 commit comments