@my-devkit/cli 1.0.0 → 1.0.1

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 (42) hide show
  1. package/README.md +54 -0
  2. package/dist/index.js +36 -8
  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 +93 -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/gist.d.ts +23 -0
  23. package/dist/utils/gist.js +111 -0
  24. package/dist/utils/gist.js.map +1 -0
  25. package/dist/utils/index.d.ts +4 -0
  26. package/dist/utils/index.js +17 -0
  27. package/dist/utils/index.js.map +1 -0
  28. package/dist/utils/npm.d.ts +25 -0
  29. package/dist/utils/npm.js +371 -0
  30. package/dist/utils/npm.js.map +1 -0
  31. package/package.json +9 -6
  32. package/src/index.ts +22 -2
  33. package/src/replace-files.ts +18 -0
  34. package/src/replace-tokens.ts +20 -0
  35. package/src/switch-to-npm.ts +24 -0
  36. package/src/switch-to-pnpm.ts +19 -0
  37. package/src/utils/args.ts +6 -0
  38. package/src/utils/config.ts +25 -0
  39. package/src/utils/gist.ts +62 -0
  40. package/src/utils/index.ts +4 -0
  41. package/src/utils/npm.ts +164 -0
  42. package/tsconfig.json +1 -1
@@ -0,0 +1,24 @@
1
+ import { config, Npm } from './utils';
2
+
3
+ const switchToNpm = async () => {
4
+ for (const pkg of config().packages) {
5
+ const npm = Npm.prefix(pkg.path);
6
+ await npm.clean();
7
+ await npm.write(['scripts', 'preinstall'], undefined);
8
+ await npm.install();
9
+
10
+ if (pkg.deploy) {
11
+ await npm.build();
12
+ await npm.publishIfNeeded();
13
+
14
+ const packageName = await npm.getName();
15
+ const newVersion = await npm.getPublishedVersion();
16
+
17
+ for (const otherPackage of config().packages) {
18
+ await Npm.prefix(otherPackage.path).updateDependency(packageName, newVersion);
19
+ }
20
+ }
21
+ }
22
+ };
23
+
24
+ export default switchToNpm;
@@ -0,0 +1,19 @@
1
+ import { config, Npm } from './utils';
2
+
3
+ const switchToPnpm = async () => {
4
+ for (const pkg of config().packages) {
5
+ const npm = Npm.prefix(pkg.path);
6
+ await npm.clean();
7
+ await npm.removePackageLock();
8
+ await npm.write('version', '1.0.0');
9
+ await npm.write(['scripts', 'preinstall'], 'npx only-allow pnpm');
10
+
11
+ const packageName = await npm.getName();
12
+
13
+ for (const otherPackage of config().packages) {
14
+ await Npm.prefix(otherPackage.path).updateDependency(packageName, 'workspace:^1.0.0');
15
+ }
16
+ }
17
+ };
18
+
19
+ export default switchToPnpm;
@@ -0,0 +1,6 @@
1
+ const _args = process.argv.filter(arg => arg.startsWith('-'))
2
+ .map((name) => ({ name, value: process.argv[process.argv.indexOf(name) + 1] }));
3
+
4
+ export const args = (option: string, defaultValue: string = null): string => {
5
+ return _args.find(arg => arg.name === option)?.value || defaultValue;
6
+ }
@@ -0,0 +1,25 @@
1
+ import * as fs from 'fs';
2
+
3
+ interface ConfigPackageFileReplacement {
4
+ replace: string;
5
+ with: string;
6
+ }
7
+
8
+ interface ConfigPackage {
9
+ path: string;
10
+ deploy: boolean;
11
+ tokensReplacements?: string[];
12
+ fileReplacements?: ConfigPackageFileReplacement[];
13
+ }
14
+
15
+ interface Config {
16
+ packages: ConfigPackage[];
17
+ }
18
+
19
+ export const config = (): Config => {
20
+ if (!fs.existsSync('./my-devkit.json')) {
21
+ throw new Error('File my-devkit.json not found!');
22
+ }
23
+
24
+ return JSON.parse(fs.readFileSync('./my-devkit.json', { encoding: 'utf-8' }));
25
+ }
@@ -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,4 @@
1
+ export * from './args';
2
+ export * from './config';
3
+ export * from './npm';
4
+ export * from './gist';
@@ -0,0 +1,164 @@
1
+ import { execSync } from 'child_process';
2
+ import * as fs from 'fs';
3
+
4
+ import { Gist } from './gist';
5
+
6
+ export class Npm {
7
+ private _verbose = false;
8
+
9
+ constructor(private prefix: string) {
10
+ }
11
+
12
+ static prefix(prefix: string) {
13
+ return new Npm(prefix);
14
+ }
15
+
16
+ public verbose(verbose = true): void {
17
+ this._verbose = verbose;
18
+ }
19
+
20
+ public async publishIfNeeded(): Promise<void> {
21
+ await this.write('version', await this.getPublishedVersion());
22
+
23
+ const localCheckSum = await this.getLocalChecksum();
24
+ const remoteCheckSum = await this.getRemoteChecksum();
25
+
26
+ if (localCheckSum === remoteCheckSum) {
27
+ console.log(`Package ${await this.getName()} is already up to date!`);
28
+ return;
29
+ }
30
+
31
+ await this.patch();
32
+
33
+ await this.publish();
34
+
35
+ await this.updateRemoteChecksum();
36
+
37
+ console.log(`Package ${await this.getName()} ${await this.getLocalVersion()} as been published!`);
38
+ }
39
+
40
+ public async clean(): Promise<void> {
41
+ this.log(`Remove ${this.prefix}/node_modules`);
42
+ if (fs.existsSync(`${this.prefix}/node_modules`)) {
43
+ fs.rmSync(`${this.prefix}/node_modules`, { recursive: true, force: true });
44
+ }
45
+
46
+ this.log(`Remove ${this.prefix}/dist`);
47
+ if (fs.existsSync(`${this.prefix}/dist`)) {
48
+ fs.rmSync(`${this.prefix}/dist`, { recursive: true, force: true });
49
+ }
50
+ fs.mkdirSync(`${this.prefix}/dist`);
51
+ }
52
+
53
+ public async removePackageLock(): Promise<void> {
54
+ if (fs.existsSync(`${this.prefix}/package-lock.json`)) {
55
+ fs.rmSync(`${this.prefix}/package-lock.json`);
56
+ }
57
+ }
58
+
59
+ public async install(): Promise<void> {
60
+ this.log('Install package dependencies');
61
+ await this.execCmd(`npm install --prefix=${this.prefix} --silent`);
62
+ }
63
+
64
+ public async build(): Promise<void> {
65
+ this.log('Build package');
66
+ await this.execCmd(`npm run build --prefix=${this.prefix} --silent`);
67
+ }
68
+
69
+ public async patch(): Promise<void> {
70
+ this.log(`Patch ${await this.getName()}: ${await this.getLocalVersion()}`);
71
+ await this.execCmd(`npm version patch --prefix=${this.prefix}`);
72
+ this.log(`New version is: ${await this.getLocalVersion()}`);
73
+ }
74
+
75
+ public async publish(): Promise<void> {
76
+ this.log(`Publish ${await this.getName()}: ${await this.getLocalVersion()}`);
77
+ await this.execCmd(`cd ${this.prefix} && npm publish --access public --silent`);
78
+ }
79
+
80
+ public async getName(): Promise<string> {
81
+ return this.read('name');
82
+ }
83
+
84
+ public async getLocalVersion(): Promise<string> {
85
+ return this.read('version');
86
+ }
87
+
88
+ public async getPublishedVersion(): Promise<string> {
89
+ const packageName = await this.getName();
90
+ return this.execCmd(`npm show ${packageName} version`);
91
+ }
92
+
93
+ public async getLocalChecksum(): Promise<string> {
94
+ await this.execCmd(`cd ${this.prefix} && npx checksum ./dist/* ./src/* > checksum.txt`);
95
+ const checkSum = (await this.execCmd(`npx checksum ${this.prefix}/checksum.txt`)).split(' ')[0];
96
+ fs.unlinkSync(`${this.prefix}/checksum.txt`);
97
+
98
+ return checkSum;
99
+ }
100
+
101
+ public async getRemoteChecksum(): Promise<string> {
102
+ const packageName = await this.getName();
103
+
104
+ return Gist.getPackageChecksum(packageName);
105
+ }
106
+
107
+ public async updateRemoteChecksum(): Promise<void> {
108
+ const packageName = await this.getName();
109
+ const checksum = await this.getLocalChecksum();
110
+
111
+ this.log(`Set new package checksum: ${packageName} = ${checksum}`);
112
+
113
+ await Gist.setPackageChecksum(packageName, checksum);
114
+ }
115
+
116
+ public async updateDependency(packageName: string, newVersion: string): Promise<void> {
117
+ for (const dependency of ['dependencies', 'devDependencies', 'peerDependencies']) {
118
+ const currentVersion = await this.read([dependency, packageName]);
119
+ if (currentVersion) {
120
+ await this.write([dependency, packageName], newVersion);
121
+ }
122
+ }
123
+ }
124
+
125
+ public async read(properties: string | string[]): Promise<string> {
126
+ properties = Array.isArray(properties) ? properties : [properties];
127
+
128
+ let object = JSON.parse(fs.readFileSync(`${this.prefix}/package.json`, 'utf8'));
129
+
130
+ let index = 0;
131
+ while (object != null && index < properties.length) {
132
+ object = object[properties[index++]]
133
+ }
134
+ return object;
135
+ }
136
+
137
+ public async write(properties: string | string[], value: string): Promise<void> {
138
+ properties = Array.isArray(properties) ? properties : [properties];
139
+
140
+ this.log(`Write ${properties.join('->')} = ${value} in ${this.prefix}/package.json`);
141
+
142
+ const data = JSON.parse(fs.readFileSync(`${this.prefix}/package.json`, 'utf8'));
143
+
144
+ let object = data; let index = 0;
145
+ while (object != null && index < (properties.length - 1)) {
146
+ object = object[properties[index++]]
147
+ }
148
+ object[properties[properties.length - 1]] = value;
149
+
150
+ fs.writeFileSync(`${this.prefix}/package.json`, JSON.stringify(data, null, 4));
151
+ }
152
+
153
+ private async execCmd(cmd: string): Promise<string> {
154
+ const response = execSync(cmd, {});
155
+ return response.toString().trim();
156
+ }
157
+
158
+ private log(...messages) {
159
+ if (this._verbose) {
160
+ console.log(' - ', ...messages);
161
+ }
162
+ }
163
+ }
164
+
package/tsconfig.json CHANGED
@@ -17,4 +17,4 @@
17
17
  "node_modules/@types"
18
18
  ]
19
19
  }
20
- }
20
+ }