|
1 | | -const path = require('path') |
2 | | -const fs = require('fs') |
3 | | -const fsp = require('fs/promises') |
4 | | -const replace = require('replace-in-file') |
| 1 | +import path from 'path'; |
| 2 | +import fs from 'fs'; |
| 3 | +import fsp from 'fs/promises'; |
| 4 | +import replace from 'replace-in-file'; |
5 | 5 |
|
6 | | -module.exports = class Helper { |
7 | | - /** |
8 | | - * Replace tag in file |
9 | | - * @param {string} filepath Filepath |
10 | | - * @param {string} tag Tag name |
11 | | - * @param {string} replacement Replacement string |
12 | | - */ |
13 | | - async replaceTagInFile (filepath, tag, replacement) { |
14 | | - try { |
15 | | - await replace({ |
16 | | - files: filepath, |
17 | | - from: new RegExp('<%= ' + tag + ' %>', 'g'), |
18 | | - to: replacement |
19 | | - }) |
20 | | - } catch (error) { |
21 | | - throw new Error('Error occurred:', error) |
22 | | - } |
| 6 | +/** |
| 7 | + * Replace tag in file |
| 8 | + * @param {string} filepath Filepath |
| 9 | + * @param {string} tag Tag name |
| 10 | + * @param {string} replacement Replacement string |
| 11 | + */ |
| 12 | +export async function replaceTagInFile (filepath, tag, replacement) { |
| 13 | + try { |
| 14 | + await replace({ |
| 15 | + files: filepath, |
| 16 | + from: new RegExp('<%= ' + tag + ' %>', 'g'), |
| 17 | + to: replacement |
| 18 | + }) |
| 19 | + } catch (error) { |
| 20 | + throw new Error('Error occurred:', error) |
23 | 21 | } |
| 22 | +} |
24 | 23 |
|
25 | | - /** |
26 | | - * Copy file or folder |
27 | | - * @param {string} source Source |
28 | | - * @param {string} destination Destination |
29 | | - */ |
30 | | - async copyFile (source, destination) { |
31 | | - try { |
32 | | - source = path.resolve(source) |
33 | | - destination = path.resolve(destination) |
| 24 | +/** |
| 25 | + * Copy file or folder |
| 26 | + * @param {string} source Source |
| 27 | + * @param {string} destination Destination |
| 28 | + */ |
| 29 | +export async function copyFile (source, destination) { |
| 30 | + try { |
| 31 | + source = path.resolve(source) |
| 32 | + destination = path.resolve(destination) |
34 | 33 |
|
35 | | - console.log(`Copying ${source} to ${destination}`) |
| 34 | + console.log(`Copying ${source} to ${destination}`) |
36 | 35 |
|
37 | | - await fsp.copyFile(source, destination); |
38 | | - } catch { |
39 | | - throw new Error(`Failed to copy file`); |
40 | | - } |
| 36 | + await fsp.copyFile(source, destination); |
| 37 | + } catch { |
| 38 | + throw new Error(`Failed to copy file`); |
41 | 39 | } |
| 40 | +} |
42 | 41 |
|
43 | | - /** |
44 | | - * Create folder |
45 | | - */ |
46 | | - async createFolder (folder) { |
47 | | - folder = path.resolve(folder) |
| 42 | +/** |
| 43 | + * Create folder |
| 44 | + */ |
| 45 | +export async function createFolder (folder) { |
| 46 | + folder = path.resolve(folder) |
48 | 47 |
|
49 | | - if (fs.existsSync(folder) === false) { |
50 | | - await fsp.mkdir(folder, { recursive: true }) |
51 | | - } |
| 48 | + if (fs.existsSync(folder) === false) { |
| 49 | + await fsp.mkdir(folder, { recursive: true }) |
52 | 50 | } |
| 51 | +} |
53 | 52 |
|
54 | 53 | /** |
55 | 54 | * Remove folder |
56 | 55 | */ |
57 | | - async removeFolder (folder) { |
58 | | - folder = path.resolve(folder) |
| 56 | +export async function removeFolder (folder) { |
| 57 | + folder = path.resolve(folder) |
59 | 58 |
|
60 | | - try { |
61 | | - if (fs.existsSync(folder) === true) { |
62 | | - await fsp.rm(folder, { recursive: true, force: true }) |
63 | | - } |
64 | | - } catch (err) { |
65 | | - throw new Error('Folder remove failed', { cause: err }) |
| 59 | + try { |
| 60 | + if (fs.existsSync(folder) === true) { |
| 61 | + await fsp.rm(folder, { recursive: true, force: true }) |
66 | 62 | } |
| 63 | + } catch (err) { |
| 64 | + throw new Error('Folder remove failed', { cause: err }) |
67 | 65 | } |
| 66 | +} |
68 | 67 |
|
69 | 68 | /** |
70 | 69 | * Remove file |
71 | 70 | */ |
72 | | - async removeFile (file) { |
73 | | - file = path.resolve(file) |
| 71 | +export async function removeFile (file) { |
| 72 | + file = path.resolve(file) |
74 | 73 |
|
75 | | - try { |
76 | | - if (fs.existsSync(file) === true) { |
77 | | - await fsp.rm(file) |
78 | | - } |
79 | | - } catch (err) { |
80 | | - throw new Error('File remove failed', { cause: err }) |
| 74 | + try { |
| 75 | + if (fs.existsSync(file) === true) { |
| 76 | + await fsp.rm(file) |
81 | 77 | } |
| 78 | + } catch (err) { |
| 79 | + throw new Error('File remove failed', { cause: err }) |
82 | 80 | } |
| 81 | +} |
83 | 82 |
|
84 | 83 | /** |
85 | 84 | * Create symlink |
86 | 85 | */ |
87 | | - createSymlink (source, destination, type = 'file') { |
88 | | - source = path.resolve(source) |
89 | | - destination = path.resolve(destination) |
| 86 | +export function createSymlink (source, destination, type = 'file') { |
| 87 | + source = path.resolve(source) |
| 88 | + destination = path.resolve(destination) |
90 | 89 |
|
91 | | - if (fs.existsSync(destination) === false) { |
92 | | - fs.symlink( |
93 | | - source, |
94 | | - destination, |
95 | | - type, (err) => err && console.log(err) |
96 | | - ) |
97 | | - } |
| 90 | + if (fs.existsSync(destination) === false) { |
| 91 | + fs.symlink( |
| 92 | + source, |
| 93 | + destination, |
| 94 | + type, (err) => err && console.log(err) |
| 95 | + ) |
98 | 96 | } |
| 97 | +} |
99 | 98 |
|
100 | 99 | /** |
101 | 100 | * Remove symlink |
102 | 101 | */ |
103 | | - async removeSymlink (symlink) { |
104 | | - symlink = path.resolve(symlink) |
| 102 | +export async function removeSymlink (symlink) { |
| 103 | + symlink = path.resolve(symlink) |
105 | 104 |
|
106 | | - fs.readlink(symlink, (err, target) => { |
107 | | - if (target !== undefined) { |
108 | | - fs.unlink(symlink, err => { |
109 | | - if (err) { |
110 | | - throw new Error('Symlink remove failed', { cause: err }) |
111 | | - } |
112 | | - }) |
113 | | - } else if (err) { |
114 | | - // console.log(err) |
115 | | - } |
116 | | - }) |
117 | | - } |
| 105 | + fs.readlink(symlink, (err, target) => { |
| 106 | + if (target !== undefined) { |
| 107 | + fs.unlink(symlink, err => { |
| 108 | + if (err) { |
| 109 | + throw new Error('Symlink remove failed', { cause: err }) |
| 110 | + } |
| 111 | + }) |
| 112 | + } else if (err) { |
| 113 | + throw new Error('readlink failed', { cause: err }) |
| 114 | + } |
| 115 | + }) |
118 | 116 | } |
0 commit comments