-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
285 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,102 @@ | ||
import fs from "fs-extra"; | ||
import { glob } from 'glob' | ||
import { glob } from "glob"; | ||
import JSON5 from "json5"; | ||
import chalk from "chalk"; | ||
import path from "path"; | ||
|
||
let action = process.argv[2]; | ||
const config = JSON5.parse(Buffer.from(process.argv[3], "base64").toString("utf-8")); | ||
const config = JSON5.parse( | ||
Buffer.from(process.argv[3], "base64").toString("utf-8") | ||
); | ||
|
||
let dest = config.dest; | ||
|
||
if (config.dest == undefined) { | ||
if (config.tags.includes("js")) { | ||
dest = config.basePath + "/wwwroot/Scripts/"; | ||
} else if (config.tags.includes("css")) { | ||
dest = config.basePath + "/wwwroot/Styles/"; | ||
} | ||
} | ||
|
||
if (config.dryRun) { | ||
action = "dry-run"; | ||
action = "dry-run"; | ||
} | ||
|
||
// console.log(`copy ${action}`, config); | ||
|
||
glob(config.source).then((files) => { | ||
if (files.length == 0) { | ||
console.log(chalk.yellow("No files to copy", config.source)); | ||
return; | ||
} | ||
|
||
if (files.length == 0) { | ||
console.log(chalk.yellow("No files to copy", config.source)); | ||
return; | ||
} | ||
|
||
const destExists = fs.existsSync(config.dest); | ||
const destExists = fs.existsSync(dest); | ||
|
||
if (destExists) { | ||
const stats = fs.lstatSync(config.dest); | ||
if (!stats.isDirectory()) { | ||
console.log(chalk.red("Destination is not a directory")); | ||
console.log("Files:", files); | ||
console.log("Destination:", config.dest); | ||
return; | ||
if (destExists) { | ||
const stats = fs.lstatSync(dest); | ||
if (!stats.isDirectory()) { | ||
console.log(chalk.red("Destination is not a directory")); | ||
console.log("Files:", files); | ||
console.log("Destination:", dest); | ||
return; | ||
} | ||
console.log( | ||
chalk.yellow( | ||
`Destination ${dest} already exists, files may be overwritten` | ||
) | ||
); | ||
} | ||
console.log(chalk.yellow(`Destination ${config.dest} already exists, files may be overwritten`)); | ||
} | ||
|
||
let baseFolder; | ||
|
||
if (config.source.indexOf("**") > 0) { | ||
baseFolder = config.source.substring(0, config.source.indexOf("**")); | ||
} | ||
let baseFolder; | ||
|
||
files.forEach((file) => { | ||
file = file.replace(/\\/g,'/'); | ||
let relativePath; | ||
if (baseFolder) { | ||
relativePath = file.replace(baseFolder, ""); | ||
} else { | ||
relativePath = path.basename(file); | ||
if (config.source.indexOf("**") > 0) { | ||
baseFolder = config.source.substring(0, config.source.indexOf("**")); | ||
} | ||
|
||
const target = path.join(config.dest, relativePath); | ||
files.forEach((file) => { | ||
file = file.replace(/\\/g, "/"); | ||
let relativePath; | ||
if (baseFolder) { | ||
relativePath = file.replace(baseFolder, ""); | ||
} else { | ||
relativePath = path.basename(file); | ||
} | ||
|
||
if (action === "dry-run") { | ||
console.log(`Dry run (${chalk.gray("from")}, ${chalk.cyan("to")})`, chalk.gray(file), chalk.cyan(target)); | ||
} else { | ||
fs.stat(file).then((stat) => { | ||
if (!stat.isDirectory()) { | ||
fs.copy(file, target) | ||
.then(() => console.log(`Copied (${chalk.gray("from")}, ${chalk.cyan("to")})`, chalk.gray(file), chalk.cyan(target))) | ||
.catch((err) => { | ||
console.log(`${chalk.red("Error copying")} (${chalk.gray("from")}, ${chalk.cyan("to")})`, chalk.gray(file), chalk.cyan(target), chalk.red(err)); | ||
throw err; | ||
const target = path.join(dest, relativePath); | ||
|
||
if (action === "dry-run") { | ||
console.log( | ||
`Dry run (${chalk.gray("from")}, ${chalk.cyan("to")})`, | ||
chalk.gray(file), | ||
chalk.cyan(target) | ||
); | ||
} else { | ||
fs.stat(file).then((stat) => { | ||
if (!stat.isDirectory()) { | ||
fs.copy(file, target) | ||
.then(() => | ||
console.log( | ||
`Copied (${chalk.gray("from")}, ${chalk.cyan( | ||
"to" | ||
)})`, | ||
chalk.gray(file), | ||
chalk.cyan(target) | ||
) | ||
) | ||
.catch((err) => { | ||
console.log( | ||
`${chalk.red("Error copying")} (${chalk.gray( | ||
"from" | ||
)}, ${chalk.cyan("to")})`, | ||
chalk.gray(file), | ||
chalk.cyan(target), | ||
chalk.red(err) | ||
); | ||
throw err; | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,147 @@ | ||
import fs from "fs-extra"; | ||
import { glob } from 'glob' | ||
import { glob } from "glob"; | ||
import JSON5 from "json5"; | ||
import chalk from "chalk"; | ||
import path from "path"; | ||
import swc from "@swc/core"; | ||
|
||
let action = process.argv[2]; | ||
const config = JSON5.parse(Buffer.from(process.argv[3], "base64").toString("utf-8")); | ||
const config = JSON5.parse( | ||
Buffer.from(process.argv[3], "base64").toString("utf-8") | ||
); | ||
|
||
let dest = config.dest; | ||
|
||
if (config.dest == undefined) { | ||
if (config.tags.includes("js")) { | ||
dest = config.basePath + "/wwwroot/Scripts/"; | ||
} else if (config.tags.includes("css")) { | ||
dest = config.basePath + "/wwwroot/Styles/"; | ||
} | ||
} | ||
|
||
if (config.dryRun) { | ||
action = "dry-run"; | ||
action = "dry-run"; | ||
} | ||
|
||
// console.log(`minify ${action}`, config); | ||
|
||
glob(config.source).then((files) => { | ||
if (files.length == 0) { | ||
console.log(chalk.yellow("No files to copy", config.source)); | ||
return; | ||
} | ||
|
||
const destExists = fs.existsSync(config.dest); | ||
|
||
if (destExists) { | ||
const stats = fs.lstatSync(config.dest); | ||
if (!stats.isDirectory()) { | ||
console.log(chalk.red("Destination is not a directory")); | ||
console.log("Files:", files); | ||
console.log("Destination:", config.dest); | ||
return; | ||
if (files.length == 0) { | ||
console.log(chalk.yellow("No files to minify", config.source)); | ||
return; | ||
} | ||
console.log(chalk.yellow(`Destination ${config.dest} already exists, files may be overwritten`)); | ||
} | ||
|
||
let baseFolder; | ||
const destExists = fs.existsSync(dest); | ||
|
||
if (config.source.indexOf("**") > 0) { | ||
baseFolder = config.source.substring(0, config.source.indexOf("**")); | ||
} | ||
if (destExists) { | ||
const stats = fs.lstatSync(dest); | ||
if (!stats.isDirectory()) { | ||
console.log(chalk.red("Destination is not a directory")); | ||
console.log("Files:", files); | ||
console.log("Destination:", dest); | ||
return; | ||
} | ||
console.log( | ||
chalk.yellow( | ||
`Destination ${dest} already exists, files may be overwritten` | ||
) | ||
); | ||
} | ||
|
||
files.forEach((file) => { | ||
file = file.replace(/\\/g, "/"); | ||
let relativePath; | ||
let baseFolder; | ||
|
||
if (baseFolder) { | ||
relativePath = file.replace(baseFolder, ""); | ||
} else { | ||
relativePath = path.basename(file); | ||
if (config.source.indexOf("**") > 0) { | ||
baseFolder = config.source.substring(0, config.source.indexOf("**")); | ||
} | ||
|
||
const target = path.join(config.dest, relativePath); | ||
|
||
if (action === "dry-run") { | ||
console.log(`Dry run (${chalk.gray("from")}, ${chalk.cyan("to")})`, chalk.gray(file), chalk.cyan(target)); | ||
} else { | ||
fs.stat(file).then(async (stat) => { | ||
if (!stat.isDirectory()) { | ||
let fileInfo = path.parse(file); | ||
|
||
if (fileInfo.ext === ".js" || fileInfo.ext === ".css") { | ||
let reader = await fs.readFile(file, "utf8"); | ||
|
||
swc | ||
.minify(reader, { | ||
compress: true, | ||
sourceMap: true, | ||
}) | ||
.then((output) => { | ||
const minifiedTarget = path.join(config.dest, path.parse(target).name + ".min.js"); | ||
fs.outputFile(minifiedTarget, output.code); | ||
console.log(`Minified (${chalk.gray("from")}, ${chalk.cyan("to")})`, chalk.gray(file), chalk.cyan(minifiedTarget)); | ||
|
||
const mappedTarget = path.join(config.dest, path.parse(target).name + ".map"); | ||
fs.outputFile(mappedTarget, output.map); | ||
console.log(`Mapped (${chalk.gray("from")}, ${chalk.cyan("to")})`, chalk.gray(file), chalk.cyan(mappedTarget)); | ||
}); | ||
|
||
fs.copy(file, target) | ||
.then(() => console.log(`Copied (${chalk.gray("from")}, ${chalk.cyan("to")})`, chalk.gray(file), chalk.cyan(target))) | ||
.catch((err) => { | ||
console.log(`${chalk.red("Error copying")} (${chalk.gray("from")}, ${chalk.cyan("to")})`, chalk.gray(file), chalk.cyan(target), chalk.red(err)); | ||
throw err; | ||
}); | ||
} else { | ||
console.log("Trying to minify a file with an extension that is not allowed."); | ||
} | ||
files.forEach((file) => { | ||
file = file.replace(/\\/g, "/"); | ||
let relativePath; | ||
|
||
if (baseFolder) { | ||
relativePath = file.replace(baseFolder, ""); | ||
} else { | ||
relativePath = path.basename(file); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
const target = path.join(dest, relativePath); | ||
|
||
if (action === "dry-run") { | ||
console.log( | ||
`Dry run (${chalk.gray("from")}, ${chalk.cyan("to")})`, | ||
chalk.gray(file), | ||
chalk.cyan(target) | ||
); | ||
} else { | ||
fs.stat(file).then(async (stat) => { | ||
if (!stat.isDirectory()) { | ||
let fileInfo = path.parse(file); | ||
|
||
if (fileInfo.ext === ".js" || fileInfo.ext === ".css") { | ||
let reader = await fs.readFile(file, "utf8"); | ||
|
||
swc.minify(reader, { | ||
compress: true, | ||
sourceMap: true, | ||
}).then((output) => { | ||
const minifiedTarget = path.join( | ||
dest, | ||
path.parse(target).name + ".min.js" | ||
); | ||
fs.outputFile(minifiedTarget, output.code); | ||
console.log( | ||
`Minified (${chalk.gray("from")}, ${chalk.cyan( | ||
"to" | ||
)})`, | ||
chalk.gray(file), | ||
chalk.cyan(minifiedTarget) | ||
); | ||
|
||
const mappedTarget = path.join( | ||
dest, | ||
path.parse(target).name + ".map" | ||
); | ||
fs.outputFile(mappedTarget, output.map); | ||
console.log( | ||
`Mapped (${chalk.gray("from")}, ${chalk.cyan( | ||
"to" | ||
)})`, | ||
chalk.gray(file), | ||
chalk.cyan(mappedTarget) | ||
); | ||
}); | ||
|
||
fs.copy(file, target) | ||
.then(() => | ||
console.log( | ||
`Copied (${chalk.gray( | ||
"from" | ||
)}, ${chalk.cyan("to")})`, | ||
chalk.gray(file), | ||
chalk.cyan(target) | ||
) | ||
) | ||
.catch((err) => { | ||
console.log( | ||
`${chalk.red( | ||
"Error copying" | ||
)} (${chalk.gray("from")}, ${chalk.cyan( | ||
"to" | ||
)})`, | ||
chalk.gray(file), | ||
chalk.cyan(target), | ||
chalk.red(err) | ||
); | ||
throw err; | ||
}); | ||
} else { | ||
console.log( | ||
"Trying to minify a file with an extension that is not allowed." | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.