Skip to content

Commit 60fc3e2

Browse files
committed
refactor: improve copy.js
Add JSDoc comments and type annotations to improve code documentation. Refactor the `copyFolderRecursive` function. Split the code into separate functions for better organization and reusability. Replace the custom `copyFileRecursive` function with the more efficient built-in `fs.copyFileSync` method. Replace the usage of `indexOf` with `includes` for better clarity and simplicity. Update string concatenation to use string templates for improved readability. Replace `var` declarations with `let` and `const`. Replace the usage of lodash's `each` method with the native `for..of` loop for better performance. Replace names of variables and functions with more understandable ones. Remove unused imports. Remove copying of Stem polyfill.
1 parent 568e80d commit 60fc3e2

File tree

1 file changed

+94
-81
lines changed

1 file changed

+94
-81
lines changed

copy.js

Lines changed: 94 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,111 @@
1-
var _ = require('lodash');
2-
var exec = require('child_process').execSync;
3-
var fs = require('fs-extra');
4-
var path = require('path');
1+
const fs = require("fs");
2+
const path = require("path");
53

6-
if (fs.existsSync("dist")) fs.removeSync("dist");
7-
fs.mkdirSync("dist");
8-
fs.mkdirSync("dist"+path.sep+"webdriver-ts");
9-
fs.copySync("webdriver-ts"+path.sep+"table.html", "dist"+path.sep+"webdriver-ts"+path.sep+"table.html");
4+
const internalExclude = ["node_modules", "elm-stuff", "project", ".DS_Store"];
5+
const rootExclude = ["dist", "node_modules", "webdriver-ts"];
106

11-
fs.copySync("index.html", "dist"+path.sep+"index.html");
12-
fs.copySync("css", "dist"+path.sep+"css");
7+
/**
8+
* Checks whether a given file or directory `name` should be included based on certain conditions.
9+
* @param {string} name
10+
* @returns {boolean}
11+
*/
12+
function shouldInclude(name) {
13+
const isBindingScala = name.includes("binding.scala");
1314

14-
var excludes = ["node_modules","elm-stuff","project",".DS_Store"]
15-
var excludedDirectories = ['css', 'dist','node_modules','webdriver-ts'];
15+
if (isBindingScala) {
16+
const isTarget = name.includes("/target");
17+
const isTargetWeb = name.includes("/target/web");
1618

17-
// http://stackoverflow.com/questions/13786160/copy-folder-recursively-in-node-js
18-
function copyFileSync( source, target ) {
19+
console.log(
20+
`File: ${name}\nIs Binding Scala: ${isBindingScala}\nIs Target: ${isTarget}\nIs Target Web: ${isTargetWeb}`
21+
);
1922

20-
var targetFile = target;
21-
22-
//if target is a directory a new file with the same name will be created
23-
if ( fs.existsSync( target ) ) {
24-
if ( fs.lstatSync( target ).isDirectory() ) {
25-
targetFile = path.join( target, path.basename( source ) );
26-
}
23+
if (isTarget) {
24+
return name.endsWith("/target") || isTargetWeb;
2725
}
26+
}
2827

29-
fs.writeFileSync(targetFile, fs.readFileSync(source));
28+
return internalExclude.every((ex) => !name.includes(ex));
3029
}
3130

32-
function include(name) {
33-
if (name.indexOf("binding.scala")>-1) {
34-
console.log('name.indexOf("binding.scala")>-1', name.indexOf("/target")>-1, name.indexOf("/target/web")>-1, name);
35-
if (name.indexOf("/target")>-1) {
36-
return name.endsWith('/target') || name.indexOf("/target/web")>-1;
37-
}
38-
}
39-
if (excludes.every(ex => name.indexOf(ex)==-1)) {
40-
// console.log("<- filter", name);
41-
return true;
42-
} else {
43-
return false;
44-
}
45-
}
31+
/**
32+
* Recursively copies the contents of one directory to another directory.
33+
* @param {string} sourcePath
34+
* @param {string} destinationPath
35+
* @returns
36+
*/
37+
function copyFolderRecursiveSync(sourcePath, destinationPath) {
38+
if (!fs.existsSync(sourcePath) || !fs.lstatSync(sourcePath).isDirectory()) {
39+
return;
40+
}
41+
42+
// Check if folder needs to be created or integrated
43+
if (!fs.existsSync(destinationPath)) {
44+
fs.mkdirSync(destinationPath);
45+
}
46+
47+
const files = fs.readdirSync(sourcePath);
48+
49+
for (const file of files) {
50+
const srcFilePath = path.join(sourcePath, file);
51+
const destFilePath = path.join(destinationPath, file);
52+
53+
if (!shouldInclude(srcFilePath)) {
54+
continue;
55+
}
4656

47-
function copyFolderRecursiveSync( source, target ) {
48-
var files = [];
57+
const fileStats = fs.lstatSync(srcFilePath);
4958

50-
//check if folder needs to be created or integrated
51-
var targetFolder = path.join( target, path.basename( source ) );
52-
if ( !fs.existsSync( targetFolder ) ) {
53-
fs.mkdirSync( targetFolder );
59+
if (fileStats.isDirectory()) {
60+
console.log(`copy dir ${srcFilePath}`);
61+
copyFolderRecursiveSync(srcFilePath, destFilePath);
62+
} else if (fileStats.isSymbolicLink()) {
63+
console.log("**** LINK");
64+
} else {
65+
fs.copyFileSync(srcFilePath, destFilePath);
5466
}
67+
}
68+
} // It will be possible to replace with `fs.cpSync` if the version of Node.js >= 16.7.0.
69+
70+
/**
71+
* Reads the contents of the root directory and recursively copies the contents of each folder, unless they are excluded.
72+
*/
73+
function processDirectories() {
74+
const directories = fs.readdirSync(".");
75+
const nonHiddenDirectories = directories.filter(
76+
(directory) => !directory.startsWith(".")
77+
);
5578

56-
//copy
57-
if ( fs.lstatSync( source ).isDirectory() ) {
58-
files = fs.readdirSync( source );
59-
files.forEach( function ( file ) {
60-
var curSource = path.join( source, file );
61-
if (include(curSource)) {
62-
if ( fs.lstatSync( curSource ).isDirectory() ) {
63-
console.log("copy dir "+curSource);
64-
copyFolderRecursiveSync( curSource, targetFolder );
65-
} else if ( fs.lstatSync( curSource ).isSymbolicLink() ) {
66-
console.log("**** LINK");
67-
} else {
68-
// console.log("copy file "+curSource);
69-
copyFileSync( curSource, targetFolder );
70-
}
71-
}
72-
} );
79+
for (const directory of nonHiddenDirectories) {
80+
if (
81+
fs.statSync(directory).isDirectory() &&
82+
!rootExclude.includes(directory)
83+
) {
84+
const dirPath = path.join("dist", directory);
85+
console.log(dirPath);
86+
fs.mkdirSync(dirPath);
87+
copyFolderRecursiveSync(directory, path.join("dist", directory));
7388
}
89+
}
90+
}
91+
92+
/**
93+
* Creates a dist directory, copies `table.html` from `webdriver-ts` and `index.html` into it,
94+
* and then starts copying the project folders recursively using `processDirectories()`.
95+
*/
96+
function copyProjectToDist() {
97+
fs.rmSync("dist", { force: true, recursive: true });
98+
fs.mkdirSync(path.join("dist", "webdriver-ts"), { recursive: true });
99+
100+
fs.copyFileSync(
101+
path.join("webdriver-ts", "table.html"),
102+
path.join("dist", "webdriver-ts", "table.html")
103+
);
104+
fs.copyFileSync("index.html", path.join("dist", "index.html"));
105+
106+
processDirectories();
74107
}
75108

76-
_.each(fs.readdirSync('.'), function(name) {
77-
if(fs.statSync(name).isDirectory() && name[0] !== '.' && excludedDirectories.indexOf(name)==-1) {
78-
console.log("dist"+path.sep+name);
79-
fs.mkdirSync("dist"+path.sep+name);
80-
copyFolderRecursiveSync(name, "dist");
81-
82-
/* fs.mkdirSync("dist"+path.sep+name);
83-
if (fs.existsSync(name+path.sep+"dist")) {
84-
fs.mkdirSync("dist"+path.sep+name+path.sep+"dist");
85-
fs.copySync(name+path.sep+"dist", "dist"+path.sep+name+path.sep+"dist");
86-
if (fs.existsSync(name+path.sep+"index.html")) {
87-
fs.copySync(name+path.sep+"index.html", "dist"+path.sep+name+path.sep+"index.html");
88-
}
89-
} else {
90-
if (fs.existsSync(name+path.sep+"index.html")) {
91-
fs.copySync(name+path.sep+"index.html", "dist"+path.sep+name+path.sep+"index.html");
92-
}
93-
} */
94-
}
95-
});
96-
97-
fs.copySync("stem-v0.2.70-non-keyed/node_modules/babel-polyfill/dist/polyfill.min.js","dist/stem-v0.2.70/node_modules/babel-polyfill/dist");
109+
copyProjectToDist();
98110

111+
module.exports = { copyProjectToDist };

0 commit comments

Comments
 (0)