Skip to content

Commit

Permalink
Default to /wwwroot/Scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Skrypt committed Jan 9, 2025
1 parent 3042b25 commit 2fc2e86
Show file tree
Hide file tree
Showing 30 changed files with 285 additions and 267 deletions.
122 changes: 78 additions & 44 deletions .scripts/assets-build-tool/copy.mjs
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;
});
}
});
}
});
}
});
});
});
193 changes: 124 additions & 69 deletions .scripts/assets-build-tool/min.mjs
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."
);
}
}
});
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"action": "sass",
"name": "admin-dashboard",
"source": "Assets/scss/dashboard.scss",
"dest": "wwwroot/Styles/",
"tags": ["admin", "dashboard", "scss"]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"action": "min",
"name": "content-types",
"source": "Assets/js/list-items-filter.js",
"dest": "wwwroot/Scripts/",
"tags": ["admin", "dashboard", "js"]
}
]
2 changes: 0 additions & 2 deletions src/OrchardCore.Modules/OrchardCore.Contents/Assets2.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
"action": "min",
"name": "content-localization",
"source": "Assets/js/audittrail-disabledcontent.js",
"dest": "wwwroot/Scripts/",
"tags": ["admin", "dashboard", "js"]
},
{
"action": "min",
"name": "content-localization",
"source": "Assets/js/content-type-check-all.js",
"dest": "wwwroot/Scripts/",
"tags": ["admin", "dashboard", "js"]
}
]
Loading

0 comments on commit 2fc2e86

Please sign in to comment.