@my-devkit/cli 1.0.0 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +54 -0
  2. package/dist/index.js +31 -15
  3. package/dist/index.js.map +1 -1
  4. package/dist/replace-files.d.ts +2 -0
  5. package/dist/replace-files.js +60 -0
  6. package/dist/replace-files.js.map +1 -0
  7. package/dist/replace-tokens.d.ts +2 -0
  8. package/dist/replace-tokens.js +63 -0
  9. package/dist/replace-tokens.js.map +1 -0
  10. package/dist/switch-to-npm.d.ts +2 -0
  11. package/dist/switch-to-npm.js +104 -0
  12. package/dist/switch-to-npm.js.map +1 -0
  13. package/dist/switch-to-pnpm.d.ts +2 -0
  14. package/dist/switch-to-pnpm.js +86 -0
  15. package/dist/switch-to-pnpm.js.map +1 -0
  16. package/dist/utils/args.d.ts +1 -0
  17. package/dist/utils/args.js +12 -0
  18. package/dist/utils/args.js.map +1 -0
  19. package/dist/utils/config.d.ts +15 -0
  20. package/dist/utils/config.js +12 -0
  21. package/dist/utils/config.js.map +1 -0
  22. package/dist/utils/file-system.d.ts +6 -0
  23. package/dist/utils/file-system.js +162 -0
  24. package/dist/utils/file-system.js.map +1 -0
  25. package/dist/utils/gist.d.ts +23 -0
  26. package/dist/utils/gist.js +111 -0
  27. package/dist/utils/gist.js.map +1 -0
  28. package/dist/utils/index.d.ts +5 -0
  29. package/dist/utils/index.js +18 -0
  30. package/dist/utils/index.js.map +1 -0
  31. package/dist/utils/npm.d.ts +25 -0
  32. package/dist/utils/npm.js +372 -0
  33. package/dist/utils/npm.js.map +1 -0
  34. package/package.json +9 -6
  35. package/src/index.ts +18 -4
  36. package/src/replace-files.ts +18 -0
  37. package/src/replace-tokens.ts +20 -0
  38. package/src/switch-to-npm.ts +28 -0
  39. package/src/switch-to-pnpm.ts +19 -0
  40. package/src/utils/args.ts +6 -0
  41. package/src/utils/config.ts +25 -0
  42. package/src/utils/file-system.ts +61 -0
  43. package/src/utils/gist.ts +62 -0
  44. package/src/utils/index.ts +5 -0
  45. package/src/utils/npm.ts +163 -0
  46. package/tsconfig.json +1 -1
@@ -0,0 +1,62 @@
1
+ import got from 'got';
2
+
3
+ export class Gist {
4
+ static username = 'BUONJG';
5
+ static password = '7a3f262a616ad6613a30755a6285d4b313603b36';
6
+ static gistId = 'b22c85715f2c52e7a4f157a91030ff14';
7
+
8
+ static async getPackages(): Promise<Gist.Packages> {
9
+ const response = await got<Gist.Gist>(`https://api.github.com/gists/${Gist.gistId}`, {
10
+ method: 'GET',
11
+ username: Gist.username,
12
+ password: Gist.password,
13
+ responseType: 'json'
14
+ });
15
+
16
+ return JSON.parse(response.body.files.packages.content) || {};
17
+ }
18
+
19
+
20
+ static async getPackageChecksum(packageName) {
21
+ const packages = await Gist.getPackages();
22
+
23
+ return packages[packageName] || null;
24
+ }
25
+
26
+ static async setPackageChecksum(packageName, checksum) {
27
+ const packages = await Gist.getPackages();
28
+ packages[packageName] = checksum;
29
+
30
+ await got(`https://api.github.com/gists/${Gist.gistId}`, {
31
+ method: 'PATCH',
32
+ username: Gist.username,
33
+ password: Gist.password,
34
+ json: {
35
+ files: {
36
+ file: {
37
+ filename: "packages",
38
+ type: "application/json",
39
+ content: JSON.stringify(packages, null, 4)
40
+ }
41
+ }
42
+ }
43
+ });
44
+ }
45
+ }
46
+
47
+ export namespace Gist {
48
+ export interface Gist {
49
+ files: {
50
+ [key: string]: {
51
+ filename: string;
52
+ size: number;
53
+ truncated: boolean;
54
+ content: string;
55
+ }
56
+ }
57
+ }
58
+
59
+ export interface Packages {
60
+ [key: string]: string;
61
+ }
62
+ }
@@ -0,0 +1,5 @@
1
+ export * from './args';
2
+ export * from './config';
3
+ export * from './npm';
4
+ export * from './gist';
5
+ export * from './file-system';
@@ -0,0 +1,163 @@
1
+ import { execSync } from 'child_process';
2
+ import * as fs from 'fs';
3
+
4
+ import { FileSystem } from './file-system';
5
+ import { Gist } from './gist';
6
+
7
+ export class Npm {
8
+ private _verbose = false;
9
+
10
+ constructor(private prefix: string) {
11
+ }
12
+
13
+ static prefix(prefix: string) {
14
+ return new Npm(prefix);
15
+ }
16
+
17
+ public verbose(verbose = true): void {
18
+ this._verbose = verbose;
19
+ }
20
+
21
+ public async publishIfNeeded(): Promise<void> {
22
+ await this.write('version', await this.getPublishedVersion());
23
+
24
+ const localCheckSum = await this.getLocalChecksum();
25
+ const remoteCheckSum = await this.getRemoteChecksum();
26
+
27
+ if (localCheckSum === remoteCheckSum) {
28
+ console.log(`Package ${await this.getName()} is already up to date!`);
29
+ return;
30
+ }
31
+
32
+ await this.patch();
33
+
34
+ await this.publish();
35
+
36
+ await this.updateRemoteChecksum(remoteCheckSum);
37
+
38
+ console.log(`Package ${await this.getName()} ${await this.getLocalVersion()} as been published!`);
39
+ }
40
+
41
+ public async clean(): Promise<void> {
42
+ this.log(`Remove ${this.prefix}/node_modules`);
43
+ if (fs.existsSync(`${this.prefix}/node_modules`)) {
44
+ fs.rmSync(`${this.prefix}/node_modules`, { recursive: true, force: true });
45
+ }
46
+
47
+ this.log(`Remove ${this.prefix}/dist`);
48
+ if (fs.existsSync(`${this.prefix}/dist`)) {
49
+ fs.rmSync(`${this.prefix}/dist`, { recursive: true, force: true });
50
+ }
51
+ fs.mkdirSync(`${this.prefix}/dist`);
52
+ }
53
+
54
+ public async removePackageLock(): Promise<void> {
55
+ if (fs.existsSync(`${this.prefix}/package-lock.json`)) {
56
+ fs.rmSync(`${this.prefix}/package-lock.json`);
57
+ }
58
+ }
59
+
60
+ public async install(): Promise<void> {
61
+ this.log('Install package dependencies');
62
+ await this.execCmd(`npm install --prefix=${this.prefix} --silent`);
63
+ }
64
+
65
+ public async build(): Promise<void> {
66
+ this.log('Build package');
67
+ await this.execCmd(`npm run build --prefix=${this.prefix} --silent`);
68
+ }
69
+
70
+ public async patch(): Promise<void> {
71
+ this.log(`Patch ${await this.getName()}: ${await this.getLocalVersion()}`);
72
+ await this.execCmd(`npm version patch --prefix=${this.prefix}`);
73
+ this.log(`New version is: ${await this.getLocalVersion()}`);
74
+ }
75
+
76
+ public async publish(): Promise<void> {
77
+ this.log(`Publish ${await this.getName()}: ${await this.getLocalVersion()}`);
78
+ await this.execCmd(`cd ${this.prefix} && npm publish --access public --silent`);
79
+ }
80
+
81
+ public async getName(): Promise<string> {
82
+ return this.read('name');
83
+ }
84
+
85
+ public async getLocalVersion(): Promise<string> {
86
+ return this.read('version');
87
+ }
88
+
89
+ public async getPublishedVersion(): Promise<string> {
90
+ const packageName = await this.getName();
91
+ return this.execCmd(`npm show ${packageName} version`);
92
+ }
93
+
94
+ public async getLocalChecksum(): Promise<string> {
95
+ const version = await this.read('version');
96
+ await this.write('version', '1.0.1');
97
+ const checksum = FileSystem.getChecksum([`${this.prefix}/dist`, `${this.prefix}/src`, `${this.prefix}/package.json`]);
98
+ await this.write('version', version);
99
+ return checksum;
100
+ }
101
+
102
+ public async getRemoteChecksum(): Promise<string> {
103
+ const packageName = await this.getName();
104
+
105
+ return Gist.getPackageChecksum(packageName);
106
+ }
107
+
108
+ public async updateRemoteChecksum(checksum: string): Promise<void> {
109
+ const packageName = await this.getName();
110
+ this.log(`Set new package checksum: ${packageName} = ${checksum}`);
111
+
112
+ await Gist.setPackageChecksum(packageName, checksum);
113
+ }
114
+
115
+ public async updateDependency(packageName: string, newVersion: string): Promise<void> {
116
+ for (const dependency of ['dependencies', 'devDependencies', 'peerDependencies']) {
117
+ const currentVersion = await this.read([dependency, packageName]);
118
+ if (currentVersion) {
119
+ await this.write([dependency, packageName], newVersion);
120
+ }
121
+ }
122
+ }
123
+
124
+ public async read(properties: string | string[]): Promise<string> {
125
+ properties = Array.isArray(properties) ? properties : [properties];
126
+
127
+ let object = JSON.parse(fs.readFileSync(`${this.prefix}/package.json`, 'utf8'));
128
+
129
+ let index = 0;
130
+ while (object != null && index < properties.length) {
131
+ object = object[properties[index++]]
132
+ }
133
+ return object;
134
+ }
135
+
136
+ public async write(properties: string | string[], value: string): Promise<void> {
137
+ properties = Array.isArray(properties) ? properties : [properties];
138
+
139
+ this.log(`Write ${properties.join('->')} = ${value} in ${this.prefix}/package.json`);
140
+
141
+ const data = JSON.parse(fs.readFileSync(`${this.prefix}/package.json`, 'utf8'));
142
+
143
+ let object = data; let index = 0;
144
+ while (object != null && index < (properties.length - 1)) {
145
+ object = object[properties[index++]]
146
+ }
147
+ object[properties[properties.length - 1]] = value;
148
+
149
+ fs.writeFileSync(`${this.prefix}/package.json`, JSON.stringify(data, null, 4));
150
+ }
151
+
152
+ private async execCmd(cmd: string): Promise<string> {
153
+ const response = execSync(cmd, {});
154
+ return response.toString().trim();
155
+ }
156
+
157
+ private log(...messages) {
158
+ if (this._verbose) {
159
+ console.log(' - ', ...messages);
160
+ }
161
+ }
162
+ }
163
+
package/tsconfig.json CHANGED
@@ -17,4 +17,4 @@
17
17
  "node_modules/@types"
18
18
  ]
19
19
  }
20
- }
20
+ }