@9of9solutions/replikator 1.0.9 → 1.0.10
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.
- package/main.js +187 -180
- package/package.json +31 -32
package/main.js
CHANGED
|
@@ -1,180 +1,187 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
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
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
1
|
+
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import chokidar from 'chokidar';
|
|
5
|
+
import inquirer from 'inquirer';
|
|
6
|
+
import url from 'url';
|
|
7
|
+
const projectRoot = process.cwd();
|
|
8
|
+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
|
|
9
|
+
const pkgPath = path.join(__dirname, 'package.json');
|
|
10
|
+
|
|
11
|
+
async function getVersion() {
|
|
12
|
+
const pkg = await fs.readJson(pkgPath, 'utf8');
|
|
13
|
+
return pkg.version;
|
|
14
|
+
}
|
|
15
|
+
//We save configuration in root package.json
|
|
16
|
+
const PACKAGE_JSON_ROOT_PATH = path.resolve(projectRoot, 'package.json');
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Recursively searches for folders that contain both
|
|
20
|
+
* a `src` subfolder and a `package.json` file, starting from `root`.
|
|
21
|
+
* Returns an array of objects: { name, src }.
|
|
22
|
+
*/
|
|
23
|
+
async function findApplications(root) {
|
|
24
|
+
const applications= [];
|
|
25
|
+
async function recurse(dir) {
|
|
26
|
+
for (const entry of await fs.readdir(dir, { withFileTypes: true })) {
|
|
27
|
+
if (!entry.isDirectory()) continue;
|
|
28
|
+
if(entry.name == "node_modules") continue;
|
|
29
|
+
const fullPath = path.join(dir, entry.name);
|
|
30
|
+
const srcDir = path.join(fullPath, 'src');
|
|
31
|
+
const pkgFile = path.join(fullPath, 'package.json');
|
|
32
|
+
let hasSrcAndPkg = false;
|
|
33
|
+
try {
|
|
34
|
+
const [srcStat, pkgStat] = await Promise.all([
|
|
35
|
+
fs.stat(srcDir),
|
|
36
|
+
fs.stat(pkgFile)
|
|
37
|
+
]);
|
|
38
|
+
hasSrcAndPkg = srcStat.isDirectory() && pkgStat.isFile();
|
|
39
|
+
} catch {
|
|
40
|
+
// either src or package.json doesn't exist, skip
|
|
41
|
+
}
|
|
42
|
+
if (hasSrcAndPkg) {
|
|
43
|
+
// read package.json to get the "name" field
|
|
44
|
+
let appName = entry.name;
|
|
45
|
+
try {
|
|
46
|
+
const pkg = JSON.parse(await fs.readFile(pkgFile, 'utf8'));
|
|
47
|
+
if (pkg.name) appName = pkg.name;
|
|
48
|
+
} catch {
|
|
49
|
+
// if JSON parse fails, stick with folder name
|
|
50
|
+
}
|
|
51
|
+
applications.push({ name: appName, src: srcDir });
|
|
52
|
+
}
|
|
53
|
+
// dive deeper
|
|
54
|
+
await recurse(fullPath);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
await recurse(root);
|
|
58
|
+
return applications;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Loads the config if it exists, or creates it via prompt.
|
|
64
|
+
* The stored config contains paths relative to process.cwd().
|
|
65
|
+
* Returns an object { src, targets, ignore } where src/targets are absolute paths.
|
|
66
|
+
*/
|
|
67
|
+
async function loadConfigOrPrompt(prompt) {
|
|
68
|
+
if (await fs.pathExists(PACKAGE_JSON_ROOT_PATH)) {
|
|
69
|
+
if(!prompt) {
|
|
70
|
+
const raw = await fs.readJson(PACKAGE_JSON_ROOT_PATH);
|
|
71
|
+
if(raw.replikator && raw.replikator.src && raw.replikator.targets && raw.replikator.ignore) {
|
|
72
|
+
return {
|
|
73
|
+
src: path.resolve(projectRoot, raw.replikator.src),
|
|
74
|
+
targets: raw.replikator.targets.map(rel => path.resolve(projectRoot, rel)),
|
|
75
|
+
ignore: raw.replikator.ignore
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
} else {
|
|
80
|
+
throw new Error(`Unable to find root package.json`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const applications = await findApplications(projectRoot);
|
|
84
|
+
const answers = await inquirer.prompt([
|
|
85
|
+
{
|
|
86
|
+
name: 'src',
|
|
87
|
+
type: 'input',
|
|
88
|
+
message: 'Path to the directory to replicate:',
|
|
89
|
+
default: 'packages/shared',
|
|
90
|
+
validate: i => fs.pathExistsSync(path.resolve(projectRoot, i)) || 'The folder does not exist'
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
name: 'targets',
|
|
94
|
+
type: 'checkbox',
|
|
95
|
+
message: 'Select the targeted applications:',
|
|
96
|
+
choices: applications.map(a => { return {name: a.name, value: a.src}}),
|
|
97
|
+
validate(ans) {
|
|
98
|
+
if (ans.length < 1) {
|
|
99
|
+
return 'You must choose at least one application.';
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
const absSrc = path.resolve(projectRoot, answers.src);
|
|
107
|
+
const absTargets = await Promise.all(answers.targets.map(async t => path.join(t, 'shared')));
|
|
108
|
+
const relSrc = path.relative(projectRoot, absSrc);
|
|
109
|
+
const relTargets = absTargets.map(abs => path.relative(projectRoot, abs));
|
|
110
|
+
|
|
111
|
+
const package_json = await fs.readJson(PACKAGE_JSON_ROOT_PATH);
|
|
112
|
+
const config = { ...package_json, replikator: { src: relSrc, targets: relTargets, ignore: []} };
|
|
113
|
+
await fs.writeJson(PACKAGE_JSON_ROOT_PATH, config, { spaces: 2 });
|
|
114
|
+
console.log(`Configuration saved in ${path.relative(projectRoot, PACKAGE_JSON_ROOT_PATH)}`);
|
|
115
|
+
|
|
116
|
+
return { src: absSrc, targets: absTargets, ignore: [] };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Starts the initial copy and then sets up the watcher.
|
|
121
|
+
*/
|
|
122
|
+
async function main() {
|
|
123
|
+
let prompt = false;
|
|
124
|
+
if(process.argv.length > 2) {
|
|
125
|
+
switch (process.argv[2]) {
|
|
126
|
+
case "init":
|
|
127
|
+
prompt = true
|
|
128
|
+
break;
|
|
129
|
+
case "run":
|
|
130
|
+
prompt = false
|
|
131
|
+
break;
|
|
132
|
+
case "clean":
|
|
133
|
+
console.log('Cleaning...');
|
|
134
|
+
console.log('\x1b[32mCleaning is complete.\x1b[0m');
|
|
135
|
+
process.exit(0);
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
console.log(`RPK / Replikator CLI tool / version ${await getVersion()}`)
|
|
139
|
+
console.log("");
|
|
140
|
+
console.log("Usage: rpk [command]");
|
|
141
|
+
console.log("");
|
|
142
|
+
console.log("Commands:");
|
|
143
|
+
console.log(" init Command used to init replikator configuration.");
|
|
144
|
+
console.log(" run Command used to run replikator.");
|
|
145
|
+
process.exit(0);
|
|
146
|
+
}
|
|
147
|
+
const { src, targets, ignore } = await loadConfigOrPrompt(prompt);
|
|
148
|
+
|
|
149
|
+
await Promise.all(
|
|
150
|
+
targets.map(dest =>
|
|
151
|
+
fs.ensureDir(dest).then(() =>
|
|
152
|
+
fs.copy(src, dest, { overwrite: true })
|
|
153
|
+
)
|
|
154
|
+
)
|
|
155
|
+
);
|
|
156
|
+
console.log('Initial copy completed.');
|
|
157
|
+
|
|
158
|
+
const watcher = chokidar.watch(src, { ignored: ignore, persistent: true });
|
|
159
|
+
watcher
|
|
160
|
+
.on('add', p => syncFile('add', p, src, targets))
|
|
161
|
+
.on('change', p => syncFile('change', p, src, targets))
|
|
162
|
+
.on('unlink', p => syncFile('unlink', p, src, targets))
|
|
163
|
+
.on('error', e => console.error('Watcher error:', e));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Syncs a file that was added/modified/deleted to all targets.
|
|
168
|
+
*/
|
|
169
|
+
async function syncFile(event, filePath, srcDir, targets) {
|
|
170
|
+
const rel = path.relative(srcDir, filePath);
|
|
171
|
+
await Promise.all(targets.map(async destRoot => {
|
|
172
|
+
const destPath = path.join(destRoot, rel);
|
|
173
|
+
if (event === 'unlink') {
|
|
174
|
+
await fs.remove(destPath);
|
|
175
|
+
console.log(`Deleted: ${rel}`);
|
|
176
|
+
} else {
|
|
177
|
+
await fs.ensureDir(path.dirname(destPath));
|
|
178
|
+
await fs.copy(filePath, destPath, { overwrite: true });
|
|
179
|
+
console.log(`${event === 'add' ? 'Added' : 'Updated'}: ${rel}`);
|
|
180
|
+
}
|
|
181
|
+
}));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
main().catch(err => {
|
|
185
|
+
console.error('Fatal error:', err);
|
|
186
|
+
process.exit(1);
|
|
187
|
+
});
|
package/package.json
CHANGED
|
@@ -1,32 +1,31 @@
|
|
|
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
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@9of9solutions/replikator",
|
|
3
|
+
"version": "1.0.10",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "main.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"replikator": "main.js",
|
|
8
|
+
"rpk": "main.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"replikator": "node main.js",
|
|
12
|
+
"rpk": "node main.js"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"main.js"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [],
|
|
18
|
+
"author": "Germain LESPINGAL",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"description": "",
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"chokidar": "^4.0.3",
|
|
23
|
+
"fs-extra": "^11.3.0"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"commander": "^13.1.0",
|
|
27
|
+
"degit": "^2.8.4",
|
|
28
|
+
"inquirer": "^8.2.6",
|
|
29
|
+
"winston": "^3.17.0"
|
|
30
|
+
}
|
|
31
|
+
}
|