-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathbuild.js
More file actions
156 lines (142 loc) · 5.35 KB
/
Copy pathbuild.js
File metadata and controls
156 lines (142 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const fs = require("fs").promises;
const { dirname } = require("path");
/**
* createDirectory
*
* Removes and recreates a directory.
*
* @param {string} directory Directory name.
*/
async function createDirectory(directory) {
await fs.rm(directory, { recursive: true, force: true });
await fs.mkdir(directory);
}
/**
* copyDirectory
*
* Copies a source folder to a destination folder.
*
* @param {string} src Source directory.
* @param {string} dest Destination directory.
*/
async function copyDirectory(src, dest) {
await fs.cp(src, dest, { recursive: true }, (err) => {
if (err)
console.error(err);
});
}
/**
* copyFile
*
* Copies a file from a source to a destination.
*
* @param {string} src Source file.
* @param {string} dest Destination file.
*/
async function copyFile(src, dest) {
await fs.mkdir(dirname(dest), { recursive: true });
await fs.copyFile(src, dest);
}
/**
* copyFiles
*
* Copies multiple files from a source to a destination.
*
* @param {string[]} files Array of files to copy.
*/
async function copyFiles(files) {
for (const file of files)
await copyFile(file.src, file.dest);
}
/**
* updateImportsInFile
*
* Reads a file and replaces a source path with a destination path.
*
* @param {Object} config - Config to update imports
* @param {string} config.src - The source path.
* @param {string} config.dest - The destination path.
* @param {string} config.file - File to read from.
*/
async function updateImportsInFile({ file, src, dest }) {
let contents = await fs.readFile(file, "utf8");
contents = contents.replaceAll(src, dest);
await fs.writeFile(file, contents);
}
/**
* updateImports
*
* Updates imports in multiple files.
*
* @param {Object} config - Config to update imports
* @param {string} config.src - The source path.
* @param {string} config.dest - The destination path.
* @param {string} config.file - Files to read from.
*/
async function updateImports({ files, src, dest }) {
for (const file of files)
await updateImportsInFile({ file, src, dest });
}
const filesToMove = [
{ src: "index.html", dest: "./dist/index.html" },
{ src: "node_modules/todomvc-css/dist/global.css", dest: "./dist/styles/global.css" },
{ src: "node_modules/todomvc-css/dist/header.css", dest: "./dist/styles/header.css" },
{ src: "node_modules/todomvc-css/dist/footer.css", dest: "./dist/styles/footer.css" },
{ src: "node_modules/todomvc-css/dist/global.constructable.js", dest: "./dist/styles/global.constructable.js" },
{ src: "node_modules/todomvc-css/dist/app.constructable.js", dest: "./dist/styles/app.constructable.js" },
{ src: "node_modules/todomvc-css/dist/topbar.constructable.js", dest: "./dist/styles/topbar.constructable.js" },
{ src: "node_modules/todomvc-css/dist/main.constructable.js", dest: "./dist/styles/main.constructable.js" },
{ src: "node_modules/todomvc-css/dist/bottombar.constructable.js", dest: "./dist/styles/bottombar.constructable.js" },
{ src: "node_modules/todomvc-css/dist/todo-list.constructable.js", dest: "./dist/styles/todo-list.constructable.js" },
{ src: "node_modules/todomvc-css/dist/todo-item.constructable.js", dest: "./dist/styles/todo-item.constructable.js" },
{ src: "node_modules/speedometer-utils/step-scheduler.mjs", dest: "./dist/src/speedometer-utils/test-invoker.mjs" },
{ src: "node_modules/speedometer-utils/step-runner.mjs", dest: "./dist/src/speedometer-utils/step-runner.mjs" },
{ src: "node_modules/speedometer-utils/params.mjs", dest: "./dist/src/speedometer-utils/params.mjs" },
{ src: "node_modules/speedometer-utils/benchmark.mjs", dest: "./dist/src/speedometer-utils/benchmark.mjs" },
{ src: "node_modules/speedometer-utils/helpers.mjs", dest: "./dist/src/speedometer-utils//helpers.mjs" },
{ src: "node_modules/speedometer-utils/translations.mjs", dest: "./dist/src/speedometer-utils/translations.mjs" },
{ src: "node_modules/speedometer-utils/todomvc-utils.mjs", dest: "./dist/src/speedometer-utils/todomvc-utils.mjs" },
];
const importsToRename = [
{
src: "node_modules/todomvc-css/dist/",
dest: "styles/",
files: ["./dist/index.html"],
},
{
src: "../../../node_modules/todomvc-css/dist/",
dest: "../../../styles/",
files: [
"./dist/src/components/todo-app/todo-app.component.js",
"./dist/src/components/todo-bottombar/todo-bottombar.component.js",
"./dist/src/components/todo-item/todo-item.component.js",
"./dist/src/components/todo-list/todo-list.component.js",
"./dist/src/components/todo-topbar/todo-topbar.component.js",
],
},
{
src: "/src/",
dest: "./",
files: ["./dist/src/index.mjs"],
},
{
src: "/node_modules/speedometer-utils/",
dest: "./speedometer-utils/",
files: ["./dist/src/index.mjs", "./dist/src/workload-test.mjs"],
},
];
const build = async () => {
// create dist folder
await createDirectory("./dist");
// copy src folder
await copyDirectory("./src", "./dist/src");
// copy files to Move
await copyFiles(filesToMove);
// rename imports files
for (const entry of importsToRename) {
const { files, src, dest } = entry;
await updateImports({ files, src, dest });
}
console.log("Done with building!");
};
build();