@9of9solutions/replikator 1.0.16 → 1.0.17

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 (2) hide show
  1. package/main.js +44 -37
  2. package/package.json +1 -1
package/main.js CHANGED
@@ -77,17 +77,17 @@ async function findApplications(root) {
77
77
  /**
78
78
  * Loads the config if it exists, or creates it via prompt.
79
79
  * The stored config contains paths relative to process.cwd().
80
- * Returns an object { src, targets, ignore } where src/targets are absolute paths.
80
+ * Returns an object { src, targets, include } where src/targets are absolute paths.
81
81
  */
82
82
  async function loadConfigOrPrompt(prompt) {
83
83
  if (await fs.pathExists(PACKAGE_JSON_ROOT_PATH)) {
84
84
  if (!prompt) {
85
85
  const raw = await fs.readJson(PACKAGE_JSON_ROOT_PATH);
86
- if (raw.replikator && raw.replikator.src && raw.replikator.targets && raw.replikator.ignore) {
86
+ if (raw.replikator && raw.replikator.src && raw.replikator.targets && raw.replikator.include) {
87
87
  return {
88
88
  src: path.resolve(projectRoot, raw.replikator.src),
89
89
  targets: raw.replikator.targets.map(rel => path.resolve(projectRoot, rel)),
90
- ignore: raw.replikator.ignore
90
+ include: raw.replikator.include
91
91
  };
92
92
  }
93
93
  }
@@ -203,11 +203,11 @@ async function loadConfigOrPrompt(prompt) {
203
203
 
204
204
 
205
205
  const package_json = await fs.readJson(PACKAGE_JSON_ROOT_PATH);
206
- const config = { ...package_json, replikator: { src: relSrc, targets: relTargets, ignore: [] } };
206
+ const config = { ...package_json, replikator: { src: relSrc, targets: relTargets, include: {} } };
207
207
  await fs.writeJson(PACKAGE_JSON_ROOT_PATH, config, { spaces: 2 });
208
208
  console.log(`Configuration saved in ${path.relative(projectRoot, PACKAGE_JSON_ROOT_PATH)}`);
209
209
 
210
- return { src: absSrc, targets: absTargets, ignore: [] };
210
+ return { src: absSrc, targets: absTargets, include: [] };
211
211
  }
212
212
 
213
213
  /**
@@ -215,17 +215,18 @@ async function loadConfigOrPrompt(prompt) {
215
215
  * @param {*} prompt - Should init config or no
216
216
  */
217
217
  async function run(prompt) {
218
- const { src, targets, ignore } = await loadConfigOrPrompt(prompt);
218
+ const { src, targets, include } = await loadConfigOrPrompt(prompt);
219
219
  await Promise.all(
220
220
  targets.map(async dest => {
221
221
  await fs.remove(dest)
222
222
  await fs.ensureDir(dest);
223
223
  const relDest = path.relative(projectRoot, dest);
224
- const patterns = ignore?.[relDest] || [];
224
+ const patterns = include?.[relDest] || [];
225
225
 
226
226
  const filterFn = srcFile => {
227
- const rel = path.relative(src, srcFile) + "/";
228
- return patterns.filter(p => rel.startsWith(p)).length === 0;
227
+ const rel = path.relative(src, srcFile);
228
+ console.log(rel)
229
+ return patterns.filter(p => rel.startsWith(p)).length > 0;
229
230
  };
230
231
 
231
232
  console.log(dest)
@@ -246,9 +247,9 @@ async function run(prompt) {
246
247
 
247
248
  const watcher = chokidar.watch(src, { persistent: true });
248
249
  watcher
249
- .on('add', p => syncFile('add', p, src, targets, ignore))
250
- .on('change', p => syncFile('change', p, src, targets, ignore))
251
- .on('unlink', p => syncFile('unlink', p, src, targets, ignore))
250
+ .on('add', p => syncFile('add', p, src, targets, include))
251
+ .on('change', p => syncFile('change', p, src, targets, include))
252
+ .on('unlink', p => syncFile('unlink', p, src, targets, include))
252
253
  .on('error', e => console.error('Watcher error:', e));
253
254
  }
254
255
 
@@ -264,7 +265,7 @@ async function showInfo() {
264
265
  console.log(" init Command used to init replikator configuration.");
265
266
  console.log(" run Command used to run replikator.");
266
267
  console.log(" update Command used to update replikator.");
267
- console.log(" exclude Command used to exclude files for selected apps.");
268
+ console.log(" include Command used to include files for selected apps.");
268
269
  process.exit(0);
269
270
  }
270
271
 
@@ -283,8 +284,8 @@ async function main() {
283
284
  case "update":
284
285
  await updateRpk();
285
286
  break;
286
- case "exclude":
287
- await excludeRpk();
287
+ case "include":
288
+ await includeRpk();
288
289
  break;
289
290
  default:
290
291
  await showInfo();
@@ -306,7 +307,7 @@ async function updateRpk() {
306
307
 
307
308
  if (ua.startsWith('pnpm')) {
308
309
  cmd = 'pnpm';
309
- args = ['add', '-g', '@9of9solutions/replikator@latest'];
310
+ args = ['add', '-g', '@9of9solutions/replikator@latest', '-w'];
310
311
  } else if (ua.startsWith('yarn')) {
311
312
  cmd = 'yarn';
312
313
  args = ['global', 'add', '@9of9solutions/replikator@latest'];
@@ -331,7 +332,7 @@ const red = txt => `\x1b[31m${txt}\x1b[0m`;
331
332
  /**
332
333
  * Syncs a file that was added/modified/deleted to all targets.
333
334
  */
334
- async function syncFile(event, filePath, srcDir, targets, ignore) {
335
+ async function syncFile(event, filePath, srcDir, targets, include) {
335
336
  const rel = path.relative(srcDir, filePath);
336
337
  await Promise.all(targets.map(async destRoot => {
337
338
  const destPath = path.join(destRoot, rel);
@@ -339,8 +340,8 @@ async function syncFile(event, filePath, srcDir, targets, ignore) {
339
340
  await fs.remove(destPath);
340
341
  } else {
341
342
  const relDest = path.relative(projectRoot, destRoot);
342
- const patterns = ignore?.[relDest] || [];
343
- if (patterns.filter(p => (rel+"/").startsWith(p)).length === 0) {
343
+ const patterns = include?.[relDest] || [];
344
+ if (patterns.filter(p => (rel).startsWith(p)).length > 0) {
344
345
  await fs.ensureDir(path.dirname(destPath));
345
346
  await fs.copy(filePath, destPath, { overwrite: true });
346
347
  if ((await fs.lstat(filePath)).isFile()) {
@@ -358,9 +359,9 @@ async function syncFile(event, filePath, srcDir, targets, ignore) {
358
359
  }
359
360
 
360
361
  /**
361
- * Setup excluded files or directories for apps
362
+ * Setup included files or directories for apps
362
363
  */
363
- async function excludeRpk() {
364
+ async function includeRpk() {
364
365
  const rawPkg = await fs.readJson(PACKAGE_JSON_ROOT_PATH, 'utf8');
365
366
  const cfg = rawPkg.replikator;
366
367
  if (!cfg || !cfg.src || !Array.isArray(cfg.targets)) {
@@ -383,7 +384,7 @@ async function excludeRpk() {
383
384
  const { selectedApps } = await inquirer.prompt([{
384
385
  type: 'checkbox',
385
386
  name: 'selectedApps',
386
- message: 'Select applications on which to apply exclusions:',
387
+ message: 'Select applications on which to apply include:',
387
388
  choices: choicesApps,
388
389
  validate(ans) {
389
390
  return ans.length > 0 ? true : 'You must choose at least one application.';
@@ -392,37 +393,43 @@ async function excludeRpk() {
392
393
 
393
394
  async function walk(dir, base = '') {
394
395
  const entries = [];
395
- for (const ent of await fs.readdir(dir, { withFileTypes: true })) {
396
+ const children = await fs.readdir(dir, { withFileTypes: true });
397
+
398
+ const hasFile = children.some(c => c.isFile() && /\.(ts|tsx)$/.test(c.name));
399
+ if (hasFile && base !== '') {
400
+ entries.push(base);
401
+ }
402
+
403
+ for (const ent of children) {
404
+ if (ent.isDirectory() && !ent.name.includes('node_modules')) {
396
405
  const rel = path.join(base, ent.name);
397
406
  const full = path.join(dir, ent.name);
398
- entries.push(rel + (ent.isDirectory() ? '/' : ''));
399
- if (ent.isDirectory()) {
400
- entries.push(...await walk(full, rel));
401
- }
407
+ entries.push(...await walk(full, rel));
408
+ }
402
409
  }
410
+
403
411
  return entries;
404
- }
412
+ }
413
+
405
414
  const allEntries = await walk(absSrc);
406
415
 
407
- const { toExclude } = await inquirer.prompt([{
416
+ const { toinclude } = await inquirer.prompt([{
408
417
  type: 'checkbox',
409
- name: 'toExclude',
410
- message: 'Select files/directories (relative to src) to exclude:',
418
+ name: 'toinclude',
419
+ message: 'Select directories (relative to src) to include:',
411
420
  choices: allEntries.map(rel => ({ name: rel, value: rel })),
412
421
  validate(ans) {
413
- return ans.length > 0 ? true : 'You must choose at least one entry to exclude.';
422
+ return ans.length > 0 ? true : 'You must choose at least one entry to include.';
414
423
  }
415
424
  }]);
416
425
 
417
- rawPkg.replikator.ignore = rawPkg.replikator.ignore || {};
426
+ rawPkg.replikator.include = rawPkg.replikator.include || {};
418
427
  for (const absT of selectedApps) {
419
428
  const relT = path.relative(projectRoot, absT);
420
- rawPkg.replikator.ignore[relT] = toExclude;
429
+ rawPkg.replikator.include[relT] = toinclude;
421
430
  }
422
- console.log(PACKAGE_JSON_ROOT_PATH)
423
- console.log(rawPkg)
424
431
  await fs.writeJson(PACKAGE_JSON_ROOT_PATH, rawPkg, { spaces: 2 });
425
- console.log(green(`Exclusion config updated for ${selectedApps.length} app(s).`));
432
+ console.log(green(`Inclusion config updated for ${selectedApps.length} app(s).`));
426
433
  }
427
434
 
428
435
  main().catch(err => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@9of9solutions/replikator",
3
- "version": "1.0.16",
3
+ "version": "1.0.17",
4
4
  "type": "module",
5
5
  "main": "main.js",
6
6
  "bin": {