simple-file-templater 1.0.3 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
package/.DS_Store CHANGED
Binary file
@@ -9,9 +9,9 @@ to: string,
9
9
  * * OR [[/myRegExp/g, 'myString'], ['myString1', 'myString2']...]
10
10
  * * DON'T forget the g flag when using regexps
11
11
  */
12
- replaceInFiles?: any[],
12
+ replaceInFiles?: [string: string | RegExp, replacement: string][],
13
13
  /** same as above but for fileNames (only valid when copying folders) */
14
- replaceInFileNames?: any[],
14
+ replaceInFileNames?: [string: string | RegExp, replacement: string][],
15
15
  /** regexp array to check against path. Eg: /node_module/ <= file paths that includes the word node_module will not be taken in account */
16
16
  ignorePaths?: any[]): string[];
17
17
  /** Inject content into a file at specified place */
package/dist/templater.js CHANGED
@@ -7,6 +7,7 @@ exports.fileToLines = exports.injector = exports.templater = void 0;
7
7
  const fs_extra_1 = __importDefault(require("fs-extra"));
8
8
  const path_1 = __importDefault(require("path"));
9
9
  const fast_glob_1 = __importDefault(require("fast-glob"));
10
+ const topkat_utils_1 = require("topkat-utils");
10
11
  /** Get the content of a folder and move it with option to replace in files or in fileNames as you go */
11
12
  function templater(
12
13
  /** absolute url of template (it can be a folder) */
@@ -24,14 +25,9 @@ replaceInFileNames = [],
24
25
  /** regexp array to check against path. Eg: /node_module/ <= file paths that includes the word node_module will not be taken in account */
25
26
  ignorePaths = []) {
26
27
  try {
27
- err500IfNotSet({ from, to, varz: replaceInFiles });
28
- // convert all replacement data to array [ [regExpToReplace, replacer], ... ]
29
- if (isObject(replaceInFiles))
30
- replaceInFiles = Object.entries(replaceInFiles);
31
- replaceInFiles.forEach(([toReplace, replacer], i, arr) => toReplace instanceof RegExp || (arr[i] = [new RegExp(toReplace, 'g'), replacer]));
32
- if (isObject(replaceInFileNames))
33
- replaceInFileNames = Object.entries(replaceInFileNames);
34
- replaceInFileNames.forEach(([toReplace, replacer], i, arr) => toReplace instanceof RegExp || (arr[i] = [new RegExp(toReplace, 'g'), replacer]));
28
+ (0, topkat_utils_1.err500IfNotSet)({ from, to, varz: replaceInFiles });
29
+ const replaceInFilesParsed = parseRegexpArray(replaceInFiles);
30
+ const replaceInFileNameArr = parseRegexpArray(replaceInFileNames);
35
31
  const createdPath = [];
36
32
  let files = [from];
37
33
  const templateIsDirectory = fs_extra_1.default.statSync(from).isDirectory();
@@ -50,11 +46,11 @@ ignorePaths = []) {
50
46
  if (templateIsDirectory) {
51
47
  const [, filePath, fileName] = fileFullPath.match(/(.*)\/(.*)$/) || [];
52
48
  const newFilePath = filePath.replace(from, to);
53
- const newFileName = replaceInFileNames.reduce((str, [toReplace, replacer]) => str.replace(toReplace, replacer), fileName);
49
+ const newFileName = replaceInFileNameArr.reduce((str, [toReplace, replacer]) => str.replace(toReplace, replacer), fileName);
54
50
  newFileFullPath = path_1.default.join(newFilePath, newFileName);
55
51
  }
56
52
  const oldFileContent = fs_extra_1.default.readFileSync(fileFullPath, 'utf-8');
57
- const newFileContent = replaceInFiles.reduce((str, [toReplace, replacer]) => str.replace(toReplace, replacer), oldFileContent);
53
+ const newFileContent = replaceInFilesParsed.reduce((str, [toReplace, replacer]) => str.replace(toReplace, replacer), oldFileContent);
58
54
  fs_extra_1.default.outputFileSync(newFileFullPath, newFileContent);
59
55
  createdPath.push(newFileFullPath);
60
56
  }
@@ -74,7 +70,7 @@ filePath, data,
74
70
  */
75
71
  after) {
76
72
  try {
77
- err500IfNotSet({ filePath, data, after });
73
+ (0, topkat_utils_1.err500IfNotSet)({ filePath, data, after });
78
74
  if (!fs_extra_1.default.existsSync(filePath))
79
75
  throw 'file for injection do not exist';
80
76
  const fileContent = fs_extra_1.default.readFileSync(filePath, 'utf-8');
@@ -111,20 +107,20 @@ ignoreInlineComments = true,
111
107
  /** Will trime each line */
112
108
  trim = true) {
113
109
  try {
114
- err500IfNotSet({ filePath });
110
+ (0, topkat_utils_1.err500IfNotSet)({ filePath });
115
111
  if (!fs_extra_1.default.existsSync(filePath))
116
112
  throw 'file for injection do not exist';
117
113
  let fileContent = fs_extra_1.default.readFileSync(filePath, 'utf-8');
118
114
  if (ignoreInlineComments)
119
115
  fileContent = fileContent.replace(/\/\/.*/g, '<$COMMENT$>');
120
116
  let lines, lineBegin = 0;
121
- if (isset(regexp) && regexp instanceof RegExp) {
117
+ if ((0, topkat_utils_1.isset)(regexp) && regexp instanceof RegExp) {
122
118
  fileContent.replace(regexp, (f, m1, index, chain) => {
123
119
  const linesBefore = chain.substring(0, index).split('\n');
124
120
  lineBegin = linesBefore.length;
125
121
  lines = m1.split('\n');
126
122
  });
127
- if (!isset(lines))
123
+ if (!(0, topkat_utils_1.isset)(lines))
128
124
  throw `regexp doesn't match the subject string`;
129
125
  }
130
126
  else {
@@ -141,14 +137,10 @@ trim = true) {
141
137
  }
142
138
  }
143
139
  exports.fileToLines = fileToLines;
144
- function isset(...elms) {
145
- return elms.every(elm => typeof elm !== 'undefined' && elm !== null);
146
- }
147
- function isObject(obj) { return isset(obj) && typeof obj === 'object' && Object.getPrototypeOf(obj) === Object.prototype; }
148
- function err500IfNotSet(objectWithVarDescription) {
149
- Object.entries(objectWithVarDescription).forEach(([name, value], i) => {
150
- if (!isset(value))
151
- throw new Error(`Param number ${i} (${name}) is not set in templater function.`);
152
- });
140
+ // ╦ ╦ ╔══╗ ╦ ╔══╗ ╔══╗ ╔══╗ ╔═══
141
+ // ╠══╣ ╠═ ║ ╠══╝ ╠═ ╠═╦╝ ╚══╗
142
+ // ╩ ╩ ╚══╝ ╚══╝ ╩ ╚══╝ ╩ ╚ ═══╝
143
+ function parseRegexpArray(arr) {
144
+ return arr.map((conf) => typeof conf[0] === 'string' ? [new RegExp((0, topkat_utils_1.escapeRegexp)(conf[0]), 'g'), conf[1]] : conf);
153
145
  }
154
146
  //# sourceMappingURL=templater.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"templater.js","sourceRoot":"","sources":["../templater.ts"],"names":[],"mappings":";;;;;;AAAA,wDAAyB;AACzB,gDAAuB;AACvB,0DAA0B;AAG1B,wGAAwG;AACxG,SAAgB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AAC;gBAAE,MAAM,yCAAyC,CAAC;SACtE;aAAM;YACH,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;SACnC;QACD,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,EAAE;YAAE,KAAK,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC;QAE7C,OAAO,KAAK;aACP,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,SAAS,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;aACjF,MAAM,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC;KAC7E;IAAC,OAAO,GAAG,EAAE;QAAE,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;KAAE;AACzC,CAAC;AAlCD,kCAkCC;AAGD,sCAAsC;AACtC,sCAAsC;AACtC,sCAAsC;AAEtC,SAAS,gBAAgB,CAAC,GAAgC;IACtD,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,IAAA,2BAAY,EAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAU,CAAC,CAAC,CAAC,IAAwB,CAAC,CAAA;AACjJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simple-file-templater",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Simple templating system for file. Pattern replacer, file name replacer...",
5
5
  "main": "./dist/templater.js",
6
6
  "scripts": {
@@ -9,7 +9,7 @@
9
9
  "bump:minor": "node node_modules/bump-simple/bump-simple.js --minor",
10
10
  "bump:patch": "node node_modules/bump-simple/bump-simple.js --patch"
11
11
  },
12
- "author": "538ROMEO",
12
+ "author": "@topkat",
13
13
  "license": "ISC",
14
14
  "repository": {
15
15
  "type": "git",
@@ -21,11 +21,12 @@
21
21
  "homepage": "https://github.com/top-kat/simple-file-templater",
22
22
  "dependencies": {
23
23
  "fast-glob": "^3.0.4",
24
- "fs-extra": "^8.1.0"
24
+ "fs-extra": "^8.1.0",
25
+ "topkat-utils": "^1.2.106"
25
26
  },
26
27
  "devDependencies": {
27
28
  "@types/node": "^22.10.1",
28
- "bump-simple": "^1.0.0"
29
+ "bump-simple": "^1.0.27"
29
30
  },
30
31
  "publishConfig": {
31
32
  "access": "public"
package/templater.ts CHANGED
@@ -1,7 +1,10 @@
1
+
2
+
1
3
  import fs from 'fs-extra'
2
4
  import path from 'path'
3
5
  import fg from 'fast-glob'
4
6
 
7
+ import { isset, err500IfNotSet, escapeRegexp } from 'topkat-utils'
5
8
 
6
9
  /** Get the content of a folder and move it with option to replace in files or in fileNames as you go */
7
10
  export function templater(
@@ -14,46 +17,44 @@ export function templater(
14
17
  * * OR [[/myRegExp/g, 'myString'], ['myString1', 'myString2']...]
15
18
  * * DON'T forget the g flag when using regexps
16
19
  */
17
- replaceInFiles = [],
20
+ replaceInFiles: [string: string | RegExp, replacement: string][] = [],
18
21
  /** same as above but for fileNames (only valid when copying folders) */
19
- replaceInFileNames = [],
22
+ replaceInFileNames: [string: string | RegExp, replacement: string][] = [],
20
23
  /** regexp array to check against path. Eg: /node_module/ <= file paths that includes the word node_module will not be taken in account */
21
24
  ignorePaths = []
22
25
  ) {
23
26
  try {
24
27
  err500IfNotSet({ from, to, varz: replaceInFiles })
25
28
 
26
- // convert all replacement data to array [ [regExpToReplace, replacer], ... ]
27
- if (isObject(replaceInFiles)) replaceInFiles = Object.entries(replaceInFiles);
28
- replaceInFiles.forEach(([toReplace, replacer], i, arr) => toReplace instanceof RegExp || (arr[i] = [new RegExp(toReplace, 'g'), replacer]));
29
- if (isObject(replaceInFileNames)) replaceInFileNames = Object.entries(replaceInFileNames);
30
- replaceInFileNames.forEach(([toReplace, replacer], i, arr) => toReplace instanceof RegExp || (arr[i] = [new RegExp(toReplace, 'g'), replacer]));
29
+ const replaceInFilesParsed = parseRegexpArray(replaceInFiles)
30
+ const replaceInFileNameArr = parseRegexpArray(replaceInFileNames)
31
31
 
32
32
  const createdPath = [] as string[]
33
33
 
34
34
  let files = [from]
35
- const templateIsDirectory = fs.statSync(from).isDirectory();
35
+ const templateIsDirectory = fs.statSync(from).isDirectory()
36
+
36
37
  // get directory structure
37
38
  if (templateIsDirectory) {
38
- if (fs.existsSync(to) && !fs.statSync(to).isDirectory()) throw '"from" argument is a directory but "to" arg is a file';
39
- files = fg.sync(`${from}/**/*`, { dot: true });
40
- } else if (fs.existsSync(to) && fs.statSync(to).isDirectory()) throw '"to" argument is a directory but "from" arg is a file';
39
+ if (fs.existsSync(to) && !fs.statSync(to).isDirectory()) throw '"from" argument is a directory but "to" arg is a file'
40
+ files = fg.sync(`${from}/**/*`, { dot: true })
41
+ } else if (fs.existsSync(to) && fs.statSync(to).isDirectory()) throw '"to" argument is a directory but "from" arg is a file'
41
42
 
42
43
  for (const fileFullPath of files) {
43
- if (ignorePaths.some(reg => reg.test(fileFullPath))) continue;
44
- let newFileFullPath = to;
44
+ if (ignorePaths.some(reg => reg.test(fileFullPath))) continue
45
+ let newFileFullPath = to
45
46
  if (templateIsDirectory) {
46
- const [, filePath, fileName] = fileFullPath.match(/(.*)\/(.*)$/) || [];
47
+ const [, filePath, fileName] = fileFullPath.match(/(.*)\/(.*)$/) || []
47
48
  const newFilePath = filePath.replace(from, to);
48
- const newFileName = replaceInFileNames.reduce((str, [toReplace, replacer]) => str.replace(toReplace, replacer), fileName);
49
- newFileFullPath = path.join(newFilePath, newFileName);
49
+ const newFileName = replaceInFileNameArr.reduce((str, [toReplace, replacer]) => str.replace(toReplace, replacer), fileName)
50
+ newFileFullPath = path.join(newFilePath, newFileName)
50
51
  }
51
- const oldFileContent = fs.readFileSync(fileFullPath, 'utf-8');
52
- const newFileContent = replaceInFiles.reduce((str, [toReplace, replacer]) => str.replace(toReplace, replacer), oldFileContent);
52
+ const oldFileContent = fs.readFileSync(fileFullPath, 'utf-8')
53
+ const newFileContent = replaceInFilesParsed.reduce((str, [toReplace, replacer]) => str.replace(toReplace, replacer), oldFileContent)
53
54
 
54
- fs.outputFileSync(newFileFullPath, newFileContent);
55
+ fs.outputFileSync(newFileFullPath, newFileContent)
55
56
 
56
- createdPath.push(newFileFullPath);
57
+ createdPath.push(newFileFullPath)
57
58
  }
58
59
 
59
60
  return createdPath
@@ -131,13 +132,10 @@ export function fileToLines(
131
132
  }
132
133
 
133
134
 
134
- function isset(...elms) {
135
- return elms.every(elm => typeof elm !== 'undefined' && elm !== null);
136
- }
137
- function isObject(obj) { return isset(obj) && typeof obj === 'object' && Object.getPrototypeOf(obj) === Object.prototype; }
135
+ // ╦ ╦ ╔══╗ ╦ ╔══╗ ╔══╗ ╔══╗ ╔═══
136
+ // ╠══╣ ╠═ ║ ╠══╝ ╠═ ╠═╦╝ ╚══╗
137
+ // ╩ ╩ ╚══╝ ╚══╝ ╩ ╚══╝ ╩ ╚ ═══╝
138
138
 
139
- function err500IfNotSet(objectWithVarDescription) {
140
- Object.entries(objectWithVarDescription).forEach(([name, value], i) => {
141
- if (!isset(value)) throw new Error(`Param number ${i} (${name}) is not set in templater function.`)
142
- })
139
+ function parseRegexpArray(arr: [string | RegExp, string][]) {
140
+ return arr.map((conf) => typeof conf[0] === 'string' ? [new RegExp(escapeRegexp(conf[0]), 'g'), conf[1]] as const : conf as [RegExp, string])
143
141
  }
package/tsconfig.json CHANGED
@@ -16,15 +16,6 @@
16
16
  "moduleResolution": "node",
17
17
  "sourceMap": true,
18
18
  "outDir": "dist",
19
- // "baseUrl": "./",
20
- "paths": {
21
- "*": [
22
- "./node_modules/*"
23
- ],
24
- "src/*": [
25
- "./src/*"
26
- ]
27
- },
28
19
  "types": [
29
20
  "node"
30
21
  ]