@9of9solutions/replikator 1.0.10 → 1.0.11
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/README.md +93 -0
- package/main.js +186 -186
- package/package.json +31 -31
package/README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# replikator
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
## Getting started
|
|
6
|
+
|
|
7
|
+
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
|
|
8
|
+
|
|
9
|
+
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
|
|
10
|
+
|
|
11
|
+
## Add your files
|
|
12
|
+
|
|
13
|
+
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
|
|
14
|
+
- [ ] [Add files using the command line](https://docs.gitlab.com/topics/git/add_files/#add-files-to-a-git-repository) or push an existing Git repository with the following command:
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
cd existing_repo
|
|
18
|
+
git remote add origin https://gitlab.com/9-of-9-solutions/replikator.git
|
|
19
|
+
git branch -M main
|
|
20
|
+
git push -uf origin main
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Integrate with your tools
|
|
24
|
+
|
|
25
|
+
- [ ] [Set up project integrations](https://gitlab.com/9-of-9-solutions/replikator/-/settings/integrations)
|
|
26
|
+
|
|
27
|
+
## Collaborate with your team
|
|
28
|
+
|
|
29
|
+
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
|
|
30
|
+
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
|
|
31
|
+
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
|
|
32
|
+
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
|
|
33
|
+
- [ ] [Set auto-merge](https://docs.gitlab.com/user/project/merge_requests/auto_merge/)
|
|
34
|
+
|
|
35
|
+
## Test and Deploy
|
|
36
|
+
|
|
37
|
+
Use the built-in continuous integration in GitLab.
|
|
38
|
+
|
|
39
|
+
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/)
|
|
40
|
+
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
|
|
41
|
+
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
|
|
42
|
+
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
|
|
43
|
+
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
|
|
44
|
+
|
|
45
|
+
***
|
|
46
|
+
|
|
47
|
+
# Editing this README
|
|
48
|
+
|
|
49
|
+
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
|
|
50
|
+
|
|
51
|
+
## Suggestions for a good README
|
|
52
|
+
|
|
53
|
+
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
|
|
54
|
+
|
|
55
|
+
## Name
|
|
56
|
+
Choose a self-explaining name for your project.
|
|
57
|
+
|
|
58
|
+
## Description
|
|
59
|
+
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
|
|
60
|
+
|
|
61
|
+
## Badges
|
|
62
|
+
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
|
|
63
|
+
|
|
64
|
+
## Visuals
|
|
65
|
+
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
|
|
66
|
+
|
|
67
|
+
## Installation
|
|
68
|
+
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
|
|
69
|
+
|
|
70
|
+
## Usage
|
|
71
|
+
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
|
|
72
|
+
|
|
73
|
+
## Support
|
|
74
|
+
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
|
|
75
|
+
|
|
76
|
+
## Roadmap
|
|
77
|
+
If you have ideas for releases in the future, it is a good idea to list them in the README.
|
|
78
|
+
|
|
79
|
+
## Contributing
|
|
80
|
+
State if you are open to contributions and what your requirements are for accepting them.
|
|
81
|
+
|
|
82
|
+
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
|
|
83
|
+
|
|
84
|
+
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
|
|
85
|
+
|
|
86
|
+
## Authors and acknowledgment
|
|
87
|
+
Show your appreciation to those who have contributed to the project.
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
For open source projects, say how it is licensed.
|
|
91
|
+
|
|
92
|
+
## Project status
|
|
93
|
+
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
|
package/main.js
CHANGED
|
@@ -1,187 +1,187 @@
|
|
|
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);
|
|
1
|
+
#!/usr/bin/env node
|
|
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
187
|
});
|
package/package.json
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@9of9solutions/replikator",
|
|
3
|
-
"version": "1.0.
|
|
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
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@9of9solutions/replikator",
|
|
3
|
+
"version": "1.0.11",
|
|
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
|
+
}
|