Skip to content

Commit fa643f7

Browse files
Rework esbuild helper script (#341)
1 parent 5200b99 commit fa643f7

File tree

4 files changed

+89
-91
lines changed

4 files changed

+89
-91
lines changed

esbuild/build.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const esbuild = require('esbuild')
2-
const Helper = require('./class/Helper.js')
3-
const helper = new Helper()
1+
import esbuild from 'esbuild'
2+
import childProcess from 'child_process'
3+
import * as helper from './class/Helper.js'
44

55
const title = 'Parrot'
66
const description = 'Viewer for tweet archives created with the Twitter Media Downloader'
77
const version = process.env.PARROT_VERSION || 'dev'
8-
const gitCommit = require('child_process')
8+
const gitCommit = childProcess
99
.execSync('git rev-parse --short HEAD')
1010
.toString()
1111
.trim()

esbuild/class/Helper.js

Lines changed: 81 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,116 @@
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';
55

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)
2321
}
22+
}
2423

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)
3433

35-
console.log(`Copying ${source} to ${destination}`)
34+
console.log(`Copying ${source} to ${destination}`)
3635

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`);
4139
}
40+
}
4241

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)
4847

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 })
5250
}
51+
}
5352

5453
/**
5554
* Remove folder
5655
*/
57-
async removeFolder (folder) {
58-
folder = path.resolve(folder)
56+
export async function removeFolder (folder) {
57+
folder = path.resolve(folder)
5958

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 })
6662
}
63+
} catch (err) {
64+
throw new Error('Folder remove failed', { cause: err })
6765
}
66+
}
6867

6968
/**
7069
* Remove file
7170
*/
72-
async removeFile (file) {
73-
file = path.resolve(file)
71+
export async function removeFile (file) {
72+
file = path.resolve(file)
7473

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)
8177
}
78+
} catch (err) {
79+
throw new Error('File remove failed', { cause: err })
8280
}
81+
}
8382

8483
/**
8584
* Create symlink
8685
*/
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)
9089

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+
)
9896
}
97+
}
9998

10099
/**
101100
* Remove symlink
102101
*/
103-
async removeSymlink (symlink) {
104-
symlink = path.resolve(symlink)
102+
export async function removeSymlink (symlink) {
103+
symlink = path.resolve(symlink)
105104

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+
})
118116
}

esbuild/watch.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const esbuild = require('esbuild')
2-
const Helper = require('./class/Helper.js')
3-
const helper = new Helper()
1+
import esbuild from 'esbuild'
2+
import * as helper from './class/Helper.js'
43

54
async function setup () {
65
// Remove and recreate dist folder

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
"autolinker": "^4.0.0",
1717
"jszip": "^3.10.1",
1818
"papaparse": "^5.4.1"
19-
}
19+
},
20+
"type": "module"
2021
}

0 commit comments

Comments
 (0)