-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync.mjs
More file actions
168 lines (141 loc) · 6.18 KB
/
sync.mjs
File metadata and controls
168 lines (141 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { createHash } from 'crypto';
import { copyFileSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, unlinkSync, writeFileSync } from 'fs';
import { basename, join } from 'path';
const changes = {
devDependencies: {},
scripts: {},
ntl: {}
};
function parseFile(filePath) {
// @ts-expect-error still working
return JSON.parse(readFileSync(filePath));
}
const pluginPackageJSON = parseFile('./package.json');
// fix tools link to use portal
if (pluginPackageJSON.dependencies?.['@nativescript-community/plugin-seed-tools'] === 'file:tools') {
pluginPackageJSON.dependencies['@nativescript-community/plugin-seed-tools'] = 'portal:tools';
} else if (pluginPackageJSON.devDependencies?.['@nativescript-community/plugin-seed-tools'] === 'file:tools') {
pluginPackageJSON.devDependencies['@nativescript-community/plugin-seed-tools'] = 'portal:tools';
}
function checkAndUpdate(json, field, packageJSON = pluginPackageJSON, replacer) {
if (typeof json === 'object' && !Array.isArray(json)) {
if (packageJSON[field] === undefined) {
packageJSON[field] = {};
}
for (const [key, value] of Object.entries(json)) {
if (typeof packageJSON[field][key] === 'undefined') {
const actualValue = replacer ? replacer(value) : value;
packageJSON[field][key] = actualValue;
} else if (JSON.stringify(value) !== JSON.stringify(packageJSON[field][key])) {
if (typeof value === 'object') {
packageJSON[field] = {};
packageJSON[field][key] = value;
} else {
const actualValue = replacer ? replacer(value) : value;
packageJSON[field][key] = actualValue;
}
}
}
} else {
packageJSON[field] = json;
}
}
function deleteProperty(obj, match) {
delete obj[match];
for (const v of Object.values(obj)) {
if (v instanceof Object) {
deleteProperty(v, match);
}
}
}
function compareFile(file1, file2) {
const file1Content = readFileSync(file1);
const file2Content = readFileSync(file2);
const hash1 = createHash('md5').update(file1Content).digest('hex');
const hash2 = createHash('md5').update(file2Content).digest('hex');
return hash1 === hash2;
}
function handleCommonFile(inFile, directory) {
const actualDirectory = directory.replace('_template', '');
const destFile = join(actualDirectory, basename(inFile));
if (lstatSync(inFile).isDirectory()) {
if (!existsSync(destFile)) {
mkdirSync(destFile);
}
handleCommonFiles(inFile, join(actualDirectory, basename(inFile), '/'));
} else {
if (!existsSync(destFile)) {
console.log(`Copying common file over: ${inFile} in ${destFile}`);
copyFileSync(inFile, destFile);
} else if (!compareFile(destFile, inFile)) {
console.log(`File: ${inFile} is different from common version, copying to ${destFile}`);
copyFileSync(inFile, destFile);
}
}
}
function handleCommonFiles(commonFiles, directory) {
readdirSync(commonFiles).forEach((file) => handleCommonFile(join(commonFiles, file), directory));
}
function removeOldFiles(files) {
for (let i = 0; i < files.length; i++) {
if (existsSync(files[i])) {
unlinkSync(files[i]);
}
}
}
handleCommonFiles('./tools/common', '');
removeOldFiles(['./.eslintrc.js']);
const pluginConfigJson = parseFile('config.json');
const pluginAngular = pluginConfigJson['angular'];
const pluginDemos = pluginConfigJson['demos'];
const commonLernaJSON = parseFile('./tools/lerna.template.json');
let lernaJSON = parseFile('./lerna.json');
lernaJSON = { version: lernaJSON.version, ...commonLernaJSON };
writeFileSync('./lerna.json', JSON.stringify(lernaJSON, null, 4) + '\n');
const commonPackageJSON = parseFile('./tools/package.json.template');
checkAndUpdate(commonPackageJSON['scripts'], 'scripts');
checkAndUpdate(commonPackageJSON['ntl'], 'ntl');
checkAndUpdate(commonPackageJSON['workspaces'], 'workspaces');
checkAndUpdate(commonPackageJSON['engines'], 'engines');
checkAndUpdate(commonPackageJSON['packageManager'], 'packageManager');
if (!pluginAngular) {
deleteProperty(pluginPackageJSON, 'build.angular');
pluginPackageJSON['scripts']['build.all'] = 'npm run build';
}
if (!pluginDemos.includes('ng')) {
deleteProperty(pluginPackageJSON, 'demo.ng.android');
deleteProperty(pluginPackageJSON, 'demo.ng.ios');
deleteProperty(pluginPackageJSON, 'demo.ng.clean');
}
if (!pluginDemos.includes('react')) {
deleteProperty(pluginPackageJSON, 'demo.react.android');
deleteProperty(pluginPackageJSON, 'demo.react.ios');
deleteProperty(pluginPackageJSON, 'demo.react.clean');
}
if (!pluginDemos.includes('svelte')) {
deleteProperty(pluginPackageJSON, 'demo.svelte.android');
deleteProperty(pluginPackageJSON, 'demo.svelte.ios');
deleteProperty(pluginPackageJSON, 'demo.svelte.clean');
}
if (!pluginDemos.includes('vue')) {
deleteProperty(pluginPackageJSON, 'demo.vue.android');
deleteProperty(pluginPackageJSON, 'demo.vue.ios');
deleteProperty(pluginPackageJSON, 'demo.vue.clean');
}
writeFileSync('./package.json', JSON.stringify(pluginPackageJSON, null, 4) + '\n');
console.log('Common files and package.json have been synced.');
// handle packages
const commonPackagesPackageJSON = parseFile('./tools/packages/package.json.template');
readdirSync('./packages').forEach((file) => {
handleCommonFiles('./tools/packages/common', join('./packages', file))
const jsonPath = join('./packages', file, 'package.json');
const packagePackageJSON = parseFile(jsonPath);
checkAndUpdate(commonPackagesPackageJSON['scripts'], 'scripts', packagePackageJSON, (v) => v.replaceAll('${PACKAGE_NAME}', file));
const pluginAngular = existsSync(join('./src', file, 'angular'));
if (!pluginAngular) {
deleteProperty(packagePackageJSON, 'build.angular');
packagePackageJSON['scripts']['build.all'] = 'npm run build';
}
writeFileSync(jsonPath, JSON.stringify(packagePackageJSON, null, 4) + '\n');
});
console.log('packages package.json have been synced.');