create-harper 0.5.1 → 0.6.0

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/lib/fs/copyDir.js CHANGED
@@ -1,18 +1,27 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
- import { copy } from './copy.js';
3
+ import { copyFile } from './copyFile.js';
4
4
 
5
5
  /**
6
6
  * Recursively copies a directory from source to destination.
7
7
  *
8
8
  * @param {string} srcDir - The source directory path.
9
9
  * @param {string} destDir - The destination directory path.
10
+ * @param {(src: string, dest: string) => boolean} [filter] - An optional filter function that returns true if the file should be included.
11
+ * @param {Record<string, string> | ((content: string, sourcePath: string, targetPath: string) => string)} [substitutions] - A mapping of strings to replace or a function that returns the updated content.
10
12
  */
11
- export function copyDir(srcDir, destDir) {
13
+ export function copyDir(srcDir, destDir, filter, substitutions) {
12
14
  fs.mkdirSync(destDir, { recursive: true });
13
15
  for (const file of fs.readdirSync(srcDir)) {
14
16
  const srcFile = path.resolve(srcDir, file);
15
17
  const destFile = path.resolve(destDir, file);
16
- copy(srcFile, destFile);
18
+ if (filter === undefined || filter(srcFile, destFile)) {
19
+ if (fs.lstatSync(srcFile).isDirectory()) {
20
+ fs.mkdirSync(destFile, { recursive: true });
21
+ copyDir(srcFile, destFile, substitutions);
22
+ } else {
23
+ copyFile(srcFile, destFile, substitutions);
24
+ }
25
+ }
17
26
  }
18
27
  }
@@ -0,0 +1,28 @@
1
+ import fs from 'node:fs';
2
+ import colors from 'picocolors';
3
+
4
+ const {
5
+ gray,
6
+ green,
7
+ } = colors;
8
+
9
+ /**
10
+ * Reads a template file, applies string substitutions, and writes the result to a target path.
11
+ *
12
+ * @param {string} sourcePath - The path to the source template file.
13
+ * @param {string} targetPath - The path where the processed file will be written.
14
+ * @param {Record<string, string> | ((content: string, sourcePath: string, targetPath: string) => string)} [substitutions] - A mapping of strings to replace or a function that returns the updated content.
15
+ */
16
+ export function copyFile(sourcePath, targetPath, substitutions) {
17
+ let updatedContent = fs.readFileSync(sourcePath, 'utf-8');
18
+ if (typeof substitutions === 'function') {
19
+ updatedContent = substitutions(updatedContent, sourcePath, targetPath);
20
+ } else if (substitutions) {
21
+ for (const variableName in substitutions) {
22
+ updatedContent = updatedContent.replaceAll(variableName, substitutions[variableName]);
23
+ }
24
+ }
25
+
26
+ console.log(' + ' + green(targetPath) + gray(' from ' + sourcePath));
27
+ fs.writeFileSync(targetPath, updatedContent);
28
+ }
@@ -1,14 +1,14 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { renameFiles } from '../constants/renameFiles.js';
4
- import { applyAndWriteTemplateFile } from './applyAndWriteTemplateFile.js';
4
+ import { copyFile } from './copyFile.js';
5
5
 
6
6
  /**
7
7
  * Recursively crawls a template directory and copies files to the target root, applying substitutions and respecting exclusions.
8
8
  *
9
9
  * @param {string} root - The current target directory.
10
10
  * @param {string} dir - The current source directory in the template.
11
- * @param {Record<string, string>} substitutions - A mapping of strings to replace in file contents.
11
+ * @param {Record<string, string> | ((content: string) => string)} substitutions - A mapping of strings to replace or a function that returns the updated content.
12
12
  * @param {string[]} [excludedFiles] - A list of relative paths to exclude from copying.
13
13
  * @param {string} [templateRootDir] - The absolute path to the root of the template directory (internal).
14
14
  */
@@ -27,7 +27,7 @@ export function crawlTemplateDir(root, dir, substitutions, excludedFiles = [], t
27
27
  fs.mkdirSync(targetPath, { recursive: true });
28
28
  crawlTemplateDir(targetPath, templatePath, substitutions, excludedFiles, templateRootDir);
29
29
  } else {
30
- applyAndWriteTemplateFile(targetPath, templatePath, substitutions);
30
+ copyFile(templatePath, targetPath, substitutions);
31
31
  }
32
32
  }
33
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-harper",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "author": {
6
6
  "name": "HarperDB",
@@ -1,16 +0,0 @@
1
- import fs from 'node:fs';
2
-
3
- /**
4
- * Reads a template file, applies string substitutions, and writes the result to a target path.
5
- *
6
- * @param {string} targetPath - The path where the processed file will be written.
7
- * @param {string} templatePath - The path to the source template file.
8
- * @param {Record<string, string>} substitutions - A mapping of strings to replace in the file content.
9
- */
10
- export function applyAndWriteTemplateFile(targetPath, templatePath, substitutions) {
11
- let updatedContent = fs.readFileSync(templatePath, 'utf-8');
12
- for (const variableName in substitutions) {
13
- updatedContent = updatedContent.replaceAll(variableName, substitutions[variableName]);
14
- }
15
- fs.writeFileSync(targetPath, updatedContent);
16
- }
package/lib/fs/copy.js DELETED
@@ -1,17 +0,0 @@
1
- import fs from 'node:fs';
2
- import { copyDir } from './copyDir.js';
3
-
4
- /**
5
- * Copies a file or directory from source to destination.
6
- *
7
- * @param {string} src - The source path.
8
- * @param {string} dest - The destination path.
9
- */
10
- export function copy(src, dest) {
11
- const stat = fs.statSync(src);
12
- if (stat.isDirectory()) {
13
- copyDir(src, dest);
14
- } else {
15
- fs.copyFileSync(src, dest);
16
- }
17
- }