fix gulpfile
This commit is contained in:
parent
3bf2612dd0
commit
a8a1e6e133
546
gulpfile.ts
546
gulpfile.ts
|
|
@ -1,29 +1,28 @@
|
||||||
import * as gulp from "gulp"
|
import * as gulp from "gulp";
|
||||||
import fs from "fs-extra"
|
import fs from "fs-extra";
|
||||||
import * as path from "path"
|
import * as path from "path";
|
||||||
import archiver from "archiver"
|
import archiver from "archiver";
|
||||||
import stringify from "json-stringify-pretty-compact"
|
import stringify from "json-stringify-pretty-compact";
|
||||||
|
|
||||||
const sourcemaps = require('gulp-sourcemaps')
|
const sourcemaps = require("gulp-sourcemaps");
|
||||||
const uglify = require('gulp-uglify')
|
const uglify = require("gulp-uglify");
|
||||||
const concat = require("gulp-concat")
|
const concat = require("gulp-concat");
|
||||||
const buffer = require('vinyl-buffer')
|
const buffer = require("vinyl-buffer");
|
||||||
const source = require('vinyl-source-stream')
|
const source = require("vinyl-source-stream");
|
||||||
const through = require('through2')
|
const through = require("through2");
|
||||||
const jsonminify = require('gulp-jsonminify')
|
const jsonminify = require("gulp-jsonminify");
|
||||||
const merge2 = require('merge2')
|
const merge2 = require("merge2");
|
||||||
|
|
||||||
const git = require('gulp-git-streamed')
|
const git = require("gulp-git-streamed");
|
||||||
|
|
||||||
const loadJson = (path: string): any => {
|
const loadJson = (path: string): any => {
|
||||||
try {
|
try {
|
||||||
let str = fs.readFileSync(path).toString()
|
let str = fs.readFileSync(path).toString();
|
||||||
return JSON.parse(str)
|
return JSON.parse(str);
|
||||||
}
|
} catch {
|
||||||
catch {
|
throw Error("Unable to load " + path);
|
||||||
throw Error("Unable to load " + path)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
import {
|
import {
|
||||||
createLiteral,
|
createLiteral,
|
||||||
|
|
@ -38,36 +37,37 @@ import {
|
||||||
TransformerFactory,
|
TransformerFactory,
|
||||||
visitEachChild,
|
visitEachChild,
|
||||||
visitNode,
|
visitNode,
|
||||||
} from "typescript"
|
} from "typescript";
|
||||||
import less from "gulp-less"
|
import less from "gulp-less";
|
||||||
|
|
||||||
import Logger from "./source/utils/Logger"
|
import Logger from "./source/utils/Logger";
|
||||||
import {ModuleData} from "@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/packages.mjs"
|
import { ModuleData } from "@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/packages.mjs";
|
||||||
import browserify from "browserify"
|
import browserify from "browserify";
|
||||||
const tsify = require("tsify")
|
import { data } from "jquery";
|
||||||
|
const tsify = require("tsify");
|
||||||
|
|
||||||
const ts = require("gulp-typescript")
|
const ts = require("gulp-typescript");
|
||||||
|
|
||||||
const argv = require("yargs").argv
|
const argv = require("yargs").argv;
|
||||||
|
|
||||||
let distPath = "dist"
|
let distPath = "dist";
|
||||||
|
|
||||||
function getConfig() {
|
function getConfig() {
|
||||||
const configPath = path.resolve(process.cwd(), "foundryconfig.json")
|
const configPath = path.resolve(process.cwd(), "foundryconfig.json");
|
||||||
let config
|
let config;
|
||||||
|
|
||||||
if (fs.existsSync(configPath)) {
|
if (fs.existsSync(configPath)) {
|
||||||
config = loadJson(configPath)
|
config = loadJson(configPath);
|
||||||
return config
|
return config;
|
||||||
} else {
|
} else {
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Manifest {
|
interface Manifest {
|
||||||
root: string
|
root: string;
|
||||||
file: ModuleData
|
file: ModuleData;
|
||||||
name: string
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const getManifest = (): Manifest | null => {
|
const getManifest = (): Manifest | null => {
|
||||||
|
|
@ -75,237 +75,211 @@ const getManifest = (): Manifest | null => {
|
||||||
root: "",
|
root: "",
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
file: {},
|
file: {},
|
||||||
name: ""
|
name: "",
|
||||||
}
|
};
|
||||||
|
|
||||||
if (fs.existsSync("source")) {
|
if (fs.existsSync("source")) {
|
||||||
json.root = "source"
|
json.root = "source";
|
||||||
} else {
|
} else {
|
||||||
json.root = distPath
|
json.root = distPath;
|
||||||
}
|
}
|
||||||
|
const modulePath = path.join(json.root, "module.json");
|
||||||
const modulePath = path.join(json.root, "module.json")
|
const systemPath = path.join(json.root, "system.json");
|
||||||
const systemPath = path.join(json.root, "system.json")
|
|
||||||
|
|
||||||
if (fs.existsSync(modulePath)) {
|
if (fs.existsSync(modulePath)) {
|
||||||
json.file = loadJson(modulePath) as ModuleData
|
json.file = loadJson(modulePath) as ModuleData;
|
||||||
json.name = "module.json"
|
json.name = "module.json";
|
||||||
} else if (fs.existsSync(systemPath)) {
|
} else if (fs.existsSync(systemPath)) {
|
||||||
json.file = loadJson(systemPath) as ModuleData
|
json.file = loadJson(systemPath) as ModuleData;
|
||||||
json.name = "system.json"
|
json.name = "system.json";
|
||||||
} else {
|
} else {
|
||||||
return null
|
return null;
|
||||||
}
|
|
||||||
|
|
||||||
return json
|
|
||||||
}
|
}
|
||||||
|
return json;
|
||||||
|
};
|
||||||
|
|
||||||
const createTransformer = (): TransformerFactory<any> => {
|
const createTransformer = (): TransformerFactory<any> => {
|
||||||
/**
|
/**
|
||||||
* @param {typescript.Node} node
|
* @param {typescript.Node} node
|
||||||
*/
|
*/
|
||||||
const shouldMutateModuleSpecifier = (node: Node): boolean => {
|
const shouldMutateModuleSpecifier = (node: Node): boolean => {
|
||||||
if (!isImportDeclaration(node) && !isExportDeclaration(node))
|
if (!isImportDeclaration(node) && !isExportDeclaration(node)) return false;
|
||||||
return false
|
if (node.moduleSpecifier === undefined) return false;
|
||||||
if (node.moduleSpecifier === undefined)
|
if (!isStringLiteral(node.moduleSpecifier)) return false;
|
||||||
return false
|
if (!node.moduleSpecifier.text.startsWith("./") && !node.moduleSpecifier.text.startsWith("../")) return false;
|
||||||
if (!isStringLiteral(node.moduleSpecifier))
|
|
||||||
return false
|
|
||||||
if (!node.moduleSpecifier.text.startsWith("./") && !node.moduleSpecifier.text.startsWith("../"))
|
|
||||||
return false
|
|
||||||
|
|
||||||
return path.extname(node.moduleSpecifier.text) === ""
|
return path.extname(node.moduleSpecifier.text) === "";
|
||||||
}
|
};
|
||||||
|
|
||||||
return (context: TransformationContext): TSTransformer<any> => {
|
return (context: TransformationContext): TSTransformer<any> => {
|
||||||
return (node: Node) => {
|
return (node: Node) => {
|
||||||
function visitor(node: Node): Node {
|
function visitor(node: Node): Node {
|
||||||
if (shouldMutateModuleSpecifier(node)) {
|
if (shouldMutateModuleSpecifier(node)) {
|
||||||
if (isImportDeclaration(node)) {
|
if (isImportDeclaration(node)) {
|
||||||
const newModuleSpecifier = createLiteral(`${(node.moduleSpecifier as LiteralExpression).text}.js`)
|
const newModuleSpecifier = createLiteral(`${(node.moduleSpecifier as LiteralExpression).text}.js`);
|
||||||
return factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, newModuleSpecifier, undefined)
|
return factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, newModuleSpecifier, undefined);
|
||||||
} else if (isExportDeclaration(node)) {
|
} else if (isExportDeclaration(node)) {
|
||||||
const newModuleSpecifier = createLiteral(`${(node.moduleSpecifier as LiteralExpression).text}.js`)
|
const newModuleSpecifier = createLiteral(`${(node.moduleSpecifier as LiteralExpression).text}.js`);
|
||||||
return factory.updateExportDeclaration(node, node.decorators, node.modifiers, false, node.exportClause, newModuleSpecifier, undefined)
|
return factory.updateExportDeclaration(node, node.decorators, node.modifiers, false, node.exportClause, newModuleSpecifier, undefined);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return visitEachChild(node, visitor, context)
|
return visitEachChild(node, visitor, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
return visitNode(node, visitor)
|
return visitNode(node, visitor);
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
const tsConfig = ts.createProject("tsconfig.json", {
|
const tsConfig = ts.createProject("tsconfig.json", {
|
||||||
getCustomTransformers: (_program: any) => ({
|
getCustomTransformers: (_program: any) => ({
|
||||||
after: [createTransformer()],
|
after: [createTransformer()],
|
||||||
}),
|
}),
|
||||||
})
|
});
|
||||||
|
|
||||||
function buildTS() {
|
function buildTS() {
|
||||||
const debug = process.env.npm_lifecycle_event !== "package"
|
const debug = process.env.npm_lifecycle_event !== "package";
|
||||||
let res = tsConfig.src()
|
let res = tsConfig.src().pipe(sourcemaps.init()).pipe(tsConfig());
|
||||||
.pipe(sourcemaps.init())
|
|
||||||
.pipe(tsConfig())
|
|
||||||
|
|
||||||
return res.js
|
return res.js.pipe(sourcemaps.write("", { debug: debug, includeContent: true, sourceRoot: "./ts/source" })).pipe(gulp.dest(distPath));
|
||||||
.pipe(sourcemaps.write('', { debug: debug, includeContent: true, sourceRoot: './ts/source' }))
|
|
||||||
.pipe(gulp.dest(distPath))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const bundleModule = () => {
|
const bundleModule = () => {
|
||||||
const debug = argv.dbg || argv.debug
|
const debug = argv.dbg || argv.debug;
|
||||||
const bsfy = browserify(path.join(__dirname, "source/index.ts"), { debug: debug })
|
const bsfy = browserify(path.join(__dirname, "source/index.ts"), { debug: debug });
|
||||||
return bsfy.on('error', Logger.err)
|
return bsfy
|
||||||
|
.on("error", Logger.err)
|
||||||
.plugin(tsify)
|
.plugin(tsify)
|
||||||
.bundle()
|
.bundle()
|
||||||
.pipe(source(path.join(distPath, "bundle.js")))
|
.pipe(source(path.join(distPath, "bundle.js")))
|
||||||
.pipe(buffer())
|
.pipe(buffer())
|
||||||
.pipe(sourcemaps.init({ loadMaps: true }))
|
.pipe(sourcemaps.init({ loadMaps: true }))
|
||||||
.pipe(uglify())
|
.pipe(uglify())
|
||||||
.pipe(sourcemaps.write('./'))
|
.pipe(sourcemaps.write("./"))
|
||||||
.pipe(gulp.dest('./'))
|
.pipe(gulp.dest("./"));
|
||||||
}
|
};
|
||||||
|
|
||||||
const buildLess = () => {
|
const buildLess = () => {
|
||||||
return gulp.src("source/style/*.less")
|
return gulp.src("source/style/*.less").pipe(less()).pipe(concat("bundle.css")).pipe(gulp.dest(distPath));
|
||||||
.pipe(less())
|
};
|
||||||
.pipe(concat("bundle.css"))
|
|
||||||
.pipe(gulp.dest(distPath))
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Pack {
|
interface Pack {
|
||||||
root: string,
|
root: string;
|
||||||
type: string,
|
type: string;
|
||||||
name: string
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const buildPack = (pack: Pack): NodeJS.ReadWriteStream => {
|
const buildPack = (pack: Pack): NodeJS.ReadWriteStream => {
|
||||||
return gulp.src(pack.root + "/" + pack.type + "/" + pack.name + "/*.json")
|
return gulp
|
||||||
|
.src(pack.root + "/" + pack.type + "/" + pack.name + "/*.json")
|
||||||
.pipe(jsonminify())
|
.pipe(jsonminify())
|
||||||
.pipe(concat(pack.name + ".db"))
|
.pipe(concat(pack.name + ".db"))
|
||||||
.pipe(gulp.dest(distPath + "/" + pack.root + "/" + pack.type))
|
.pipe(gulp.dest(distPath + "/" + pack.root + "/" + pack.type));
|
||||||
}
|
};
|
||||||
|
|
||||||
const buildPacks = () => {
|
const buildPacks = () => {
|
||||||
let packs: Pack[] = []
|
let packs: Pack[] = [];
|
||||||
|
|
||||||
const rootDir = "packs"
|
const rootDir = "packs";
|
||||||
const packTypes = fs.readdirSync(rootDir).filter(p => fs.statSync(path.join(rootDir, p)).isDirectory())
|
const packTypes = fs.readdirSync(rootDir).filter((p) => fs.statSync(path.join(rootDir, p)).isDirectory());
|
||||||
packTypes.forEach(packType => {
|
packTypes.forEach((packType) => {
|
||||||
const packDir = path.join(rootDir, packType)
|
const packDir = path.join(rootDir, packType);
|
||||||
const packNames = fs.readdirSync(packDir).filter(p => fs.statSync(path.join(packDir, p)).isDirectory())
|
const packNames = fs.readdirSync(packDir).filter((p) => fs.statSync(path.join(packDir, p)).isDirectory());
|
||||||
packNames.forEach(packName => {
|
packNames.forEach((packName) => {
|
||||||
packs.push({
|
packs.push({
|
||||||
name: packName,
|
name: packName,
|
||||||
type: packType,
|
type: packType,
|
||||||
root: rootDir
|
root: rootDir,
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
return merge2(packs.map(p => buildPack(p)))
|
return merge2(packs.map((p) => buildPack(p)));
|
||||||
}
|
};
|
||||||
|
|
||||||
const copyFiles = async () => {
|
const copyFiles = async () => {
|
||||||
const recursiveFileSearch = (dir: string, callback: (err: NodeJS.ErrnoException | null, res: Array<string>) => void) => {
|
const recursiveFileSearch = (dir: string, callback: (err: NodeJS.ErrnoException | null, res: Array<string>) => void) => {
|
||||||
let results: Array<string> = []
|
let results: Array<string> = [];
|
||||||
fs.readdir(dir, (err, list) => {
|
fs.readdir(dir, (err, list) => {
|
||||||
if (err)
|
if (err) return callback(err, results);
|
||||||
return callback(err, results)
|
|
||||||
|
|
||||||
let pending = list.length
|
let pending = list.length;
|
||||||
if (!pending)
|
if (!pending) return callback(null, results);
|
||||||
return callback(null, results)
|
|
||||||
|
|
||||||
for (let file of list) {
|
for (let file of list) {
|
||||||
file = path.resolve(dir, file)
|
file = path.resolve(dir, file);
|
||||||
fs.stat(file, (err, stat) => {
|
fs.stat(file, (err, stat) => {
|
||||||
if (stat && stat.isDirectory()) {
|
if (stat && stat.isDirectory()) {
|
||||||
recursiveFileSearch(file, (err, res) => {
|
recursiveFileSearch(file, (err, res) => {
|
||||||
results = results.concat(res)
|
results = results.concat(res);
|
||||||
if (!--pending)
|
if (!--pending) callback(null, results);
|
||||||
callback(null, results)
|
});
|
||||||
})
|
} else {
|
||||||
|
results.push(file);
|
||||||
|
if (!--pending) callback(null, results);
|
||||||
}
|
}
|
||||||
else {
|
});
|
||||||
results.push(file)
|
|
||||||
if (!--pending)
|
|
||||||
callback(null, results)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
try {
|
try {
|
||||||
|
const modulePath = path.join("source", "module.json");
|
||||||
|
if (fs.existsSync(modulePath)) await fs.copyFile(modulePath, path.join(distPath, "module.json"));
|
||||||
|
|
||||||
const modulePath = path.join("source", "module.json")
|
const systemPath = path.join("source/system.json");
|
||||||
if (fs.existsSync(modulePath))
|
if (fs.existsSync(systemPath)) await fs.copyFile(systemPath, path.join(distPath, "system.json"));
|
||||||
await fs.copyFile(modulePath, path.join(distPath, "module.json"))
|
|
||||||
|
|
||||||
const systemPath = path.join("source/system.json")
|
if (!fs.existsSync(path.resolve(__dirname, "assets"))) return Promise.resolve();
|
||||||
if (fs.existsSync(systemPath))
|
|
||||||
await fs.copyFile(systemPath, path.join(distPath, "system.json"))
|
|
||||||
|
|
||||||
if (!fs.existsSync(path.resolve(__dirname, "assets")))
|
|
||||||
return Promise.resolve()
|
|
||||||
|
|
||||||
const filter = (src: string, dest: string): boolean => {
|
const filter = (src: string, dest: string): boolean => {
|
||||||
Logger.ok("Copying file: " + dest)
|
Logger.ok("Copying file: " + dest);
|
||||||
return true
|
return true;
|
||||||
}
|
};
|
||||||
|
|
||||||
await fs.copyFile(path.join("source", "template.json"), path.join(distPath, "template.json"))
|
await fs.copyFile(path.join("source", "template.json"), path.join(distPath, "template.json"));
|
||||||
|
|
||||||
fs.copySync(path.resolve(__dirname, "assets"), path.resolve(__dirname, distPath + "/assets"), { overwrite: true, filter })
|
fs.copySync(path.resolve(__dirname, "assets"), path.resolve(__dirname, distPath + "/assets"), { overwrite: true, filter });
|
||||||
fs.copySync(path.resolve(__dirname, "lang"), path.resolve(__dirname, distPath + "/lang"), { overwrite: true, filter })
|
fs.copySync(path.resolve(__dirname, "lang"), path.resolve(__dirname, distPath + "/lang"), { overwrite: true, filter });
|
||||||
//fs.copySync(path.resolve(__dirname, "packs"), path.resolve(__dirname, distPath + "/packs"), { overwrite: true, filter })
|
//fs.copySync(path.resolve(__dirname, "packs"), path.resolve(__dirname, distPath + "/packs"), { overwrite: true, filter })
|
||||||
fs.copySync(path.resolve(__dirname, "templates"), path.resolve(__dirname, distPath + "/templates"), { overwrite: true, filter })
|
fs.copySync(path.resolve(__dirname, "templates"), path.resolve(__dirname, distPath + "/templates"), { overwrite: true, filter });
|
||||||
return Promise.resolve()
|
return Promise.resolve();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await Promise.reject(err)
|
await Promise.reject(err);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const cleanDist = async () => {
|
const cleanDist = async () => {
|
||||||
if (argv.dbg || argv.debug)
|
if (argv.dbg || argv.debug) return;
|
||||||
return
|
Logger.log("Cleaning dist file clutter");
|
||||||
Logger.log("Cleaning dist file clutter")
|
|
||||||
|
|
||||||
const files: string[] = []
|
const files: string[] = [];
|
||||||
const getFiles = async (dir: string) => {
|
const getFiles = async (dir: string) => {
|
||||||
const arr = await fs.promises.readdir(dir)
|
const arr = await fs.promises.readdir(dir);
|
||||||
for(const entry of arr)
|
for (const entry of arr) {
|
||||||
{
|
const fullPath = path.join(dir, entry);
|
||||||
const fullPath = path.join(dir, entry)
|
const stat = await fs.promises.stat(fullPath);
|
||||||
const stat = await fs.promises.stat(fullPath)
|
if (stat.isDirectory()) await getFiles(fullPath);
|
||||||
if (stat.isDirectory())
|
else files.push(fullPath);
|
||||||
await getFiles(fullPath)
|
|
||||||
else
|
|
||||||
files.push(fullPath)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
await getFiles(path.resolve(distPath))
|
await getFiles(path.resolve(distPath));
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
if (file.endsWith("bundle.js") || file.endsWith(".css") || file.endsWith("module.json"))
|
if (file.endsWith("bundle.js") || file.endsWith(".css") || file.endsWith("module.json")) continue;
|
||||||
continue
|
|
||||||
|
|
||||||
Logger.warn("Cleaning " + path.relative(process.cwd(), file))
|
Logger.warn("Cleaning " + path.relative(process.cwd(), file));
|
||||||
await fs.promises.unlink(file)
|
await fs.promises.unlink(file);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Watch for changes for each build step
|
* Watch for changes for each build step
|
||||||
*/
|
*/
|
||||||
const buildWatch = () => {
|
const buildWatch = () => {
|
||||||
gulp.watch("source/**/*.ts", { ignoreInitial: false }, gulp.series(buildTS, bundleModule))
|
gulp.watch("source/**/*.ts", { ignoreInitial: false }, gulp.series(buildTS, bundleModule));
|
||||||
gulp.watch("source/**/*.less", { ignoreInitial: false }, buildLess)
|
gulp.watch("source/**/*.less", { ignoreInitial: false }, buildLess);
|
||||||
gulp.watch("packs", { ignoreInitial: false }, buildPacks)
|
gulp.watch("packs", { ignoreInitial: false }, buildPacks);
|
||||||
gulp.watch(["assets", "lang", "templates", "source/*.json"], { ignoreInitial: false }, copyFiles)
|
gulp.watch(["assets", "lang", "templates", "source/*.json"], { ignoreInitial: false }, copyFiles);
|
||||||
}
|
};
|
||||||
|
|
||||||
/********************/
|
/********************/
|
||||||
/* CLEAN */
|
/* CLEAN */
|
||||||
|
|
@ -316,67 +290,63 @@ const buildWatch = () => {
|
||||||
* while ignoring source files
|
* while ignoring source files
|
||||||
*/
|
*/
|
||||||
const clean = async () => {
|
const clean = async () => {
|
||||||
if (!fs.existsSync(distPath))
|
if (!fs.existsSync(distPath)) fs.mkdirSync(distPath);
|
||||||
fs.mkdirSync(distPath)
|
|
||||||
else {
|
else {
|
||||||
// Attempt to remove the files
|
// Attempt to remove the files
|
||||||
try {
|
try {
|
||||||
fs.rmSync(distPath, { recursive: true, force: true })
|
fs.rmSync(distPath, { recursive: true, force: true });
|
||||||
fs.mkdirSync(distPath)
|
fs.mkdirSync(distPath);
|
||||||
return Promise.resolve()
|
return Promise.resolve();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await Promise.reject(err)
|
await Promise.reject(err);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const setTargetDir = async () => {
|
const setTargetDir = async () => {
|
||||||
const dp = process.env.FOUNDRY_PATH
|
const dp = process.env.FOUNDRY_PATH;
|
||||||
if (!dp)
|
if (!dp) throw Error("FOUNDRY_PATH not defined in environment");
|
||||||
throw Error("FOUNDRY_PATH not defined in environment")
|
|
||||||
|
|
||||||
const name = getManifest()!.file.name ?? "midgard5"
|
const name = getManifest()!.file.name ?? "midgard5";
|
||||||
distPath = path.join(dp, "Data", "systems", name)
|
distPath = path.join(dp, "Data", "systems", name);
|
||||||
}
|
};
|
||||||
|
|
||||||
const linkUserData = async () => {
|
const linkUserData = async () => {
|
||||||
const name = getManifest()!.file.name
|
const name = getManifest()!.file.name;
|
||||||
|
|
||||||
let destDir
|
let destDir;
|
||||||
try {
|
try {
|
||||||
if (fs.existsSync(path.resolve(".", distPath, "module.json")) || fs.existsSync(path.resolve(".", "source", "module.json"))) {
|
if (fs.existsSync(path.resolve(".", distPath, "module.json")) || fs.existsSync(path.resolve(".", "source", "module.json"))) {
|
||||||
destDir = "modules"
|
destDir = "modules";
|
||||||
} else if (fs.existsSync(path.resolve(".", distPath, "system.json")) || fs.existsSync(path.resolve(".", "source", "system.json"))) {
|
} else if (fs.existsSync(path.resolve(".", distPath, "system.json")) || fs.existsSync(path.resolve(".", "source", "system.json"))) {
|
||||||
destDir = "systems"
|
destDir = "systems";
|
||||||
} else {
|
} else {
|
||||||
throw Error(`Could not find module.json or system.json`)
|
throw Error(`Could not find module.json or system.json`);
|
||||||
}
|
}
|
||||||
|
|
||||||
let linkDir
|
let linkDir;
|
||||||
const dataPath = process.env.FOUNDRY_PATH
|
const dataPath = process.env.FOUNDRY_PATH;
|
||||||
if (dataPath) {
|
if (dataPath) {
|
||||||
if (!fs.existsSync(path.join(dataPath, "Data")))
|
if (!fs.existsSync(path.join(dataPath, "Data"))) throw Error("User Data path invalid, no Data directory found");
|
||||||
throw Error("User Data path invalid, no Data directory found")
|
linkDir = path.join(dataPath, "Data", destDir, name as string);
|
||||||
|
|
||||||
linkDir = path.join(dataPath, "Data", destDir, name as string)
|
|
||||||
} else {
|
} else {
|
||||||
throw Error("FOUNDRY_PATH not defined in environment")
|
throw Error("FOUNDRY_PATH not defined in environment");
|
||||||
}
|
}
|
||||||
|
|
||||||
//if (argv.clean || argv.c) {
|
//if (argv.clean || argv.c) {
|
||||||
Logger.warn(`Removing build in ${linkDir}`)
|
Logger.warn(`Removing build in ${linkDir}`);
|
||||||
fs.rmSync(linkDir, { recursive: true, force: true })
|
fs.rmSync(linkDir, { recursive: true, force: true });
|
||||||
fs.mkdirSync(linkDir)
|
fs.mkdirSync(linkDir);
|
||||||
//}
|
//}
|
||||||
|
|
||||||
Logger.ok(`Copying build to ${linkDir}`)
|
Logger.ok(`Copying build to ${linkDir}`);
|
||||||
fs.copySync(path.resolve(distPath), linkDir, { overwrite: true })
|
fs.copySync(path.resolve(distPath), linkDir, { overwrite: true });
|
||||||
|
|
||||||
return Promise.resolve()
|
return Promise.resolve();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await Promise.reject(err)
|
await Promise.reject(err);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/*********************/
|
/*********************/
|
||||||
/* PACKAGE */
|
/* PACKAGE */
|
||||||
|
|
@ -386,48 +356,51 @@ const linkUserData = async () => {
|
||||||
* Package build
|
* Package build
|
||||||
*/
|
*/
|
||||||
async function packageBuild() {
|
async function packageBuild() {
|
||||||
const manifest = getManifest()
|
const manifest = getManifest();
|
||||||
if (manifest === null) {
|
if (manifest === null) {
|
||||||
Logger.err("Manifest file could not be loaded.")
|
Logger.err("Manifest file could not be loaded.");
|
||||||
throw Error()
|
throw Error();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
// Remove the package dir without doing anything else
|
// Remove the package dir without doing anything else
|
||||||
if (argv.clean || argv.c) {
|
if (argv.clean || argv.c) {
|
||||||
Logger.warn("Removing all packaged files")
|
Logger.warn("Removing all packaged files");
|
||||||
fs.rmSync(distPath, { force: true, recursive: true })
|
fs.rmSync(distPath, { force: true, recursive: true });
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure there is a directory to hold all the packaged versions
|
// Ensure there is a directory to hold all the packaged versions
|
||||||
if(!fs.existsSync(distPath))
|
if (!fs.existsSync(distPath)) fs.mkdirSync(distPath);
|
||||||
fs.mkdirSync(distPath)
|
|
||||||
|
|
||||||
// Initialize the zip file
|
// Initialize the zip file
|
||||||
const zipName = `${manifest.file.name}-v${manifest.file.version}.zip`
|
const zipName = `${manifest.file.name}-v${manifest.file.version}.zip`;
|
||||||
const zipFile = fs.createWriteStream(path.join(distPath, zipName))
|
const zipFile = fs.createWriteStream(zipName);
|
||||||
const zip = archiver("zip", { zlib: { level: 9 } })
|
const zip = archiver("zip", { zlib: { level: 9 } });
|
||||||
|
|
||||||
zipFile.on("close", () => {
|
zipFile.on("close", () => {
|
||||||
Logger.ok(zip.pointer() + " total bytes")
|
Logger.ok(zip.pointer() + " total bytes");
|
||||||
Logger.ok(`Zip file ${zipName} has been written`)
|
Logger.ok(`Zip file ${zipName} has been written`);
|
||||||
return resolve(true)
|
return resolve(true);
|
||||||
})
|
});
|
||||||
|
|
||||||
|
zipFile.on("end", function () {
|
||||||
|
throw "Data has been drained";
|
||||||
|
});
|
||||||
|
|
||||||
zip.on("error", (err) => {
|
zip.on("error", (err) => {
|
||||||
throw err
|
throw err;
|
||||||
})
|
});
|
||||||
|
|
||||||
zip.pipe(zipFile)
|
zip.pipe(zipFile);
|
||||||
|
|
||||||
zip.directory(path.join(process.cwd(), distPath), false)
|
zip.directory(distPath, manifest.file.name);
|
||||||
return zip.finalize()
|
|
||||||
|
return zip.finalize();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return reject(err)
|
return reject(err);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/*********************/
|
/*********************/
|
||||||
|
|
@ -438,116 +411,113 @@ async function packageBuild() {
|
||||||
* Update version and URLs in the manifest JSON
|
* Update version and URLs in the manifest JSON
|
||||||
*/
|
*/
|
||||||
const updateManifest = (cb: any) => {
|
const updateManifest = (cb: any) => {
|
||||||
const packageJson = loadJson("package.json")
|
const packageJson = loadJson("package.json");
|
||||||
const config = getConfig(),
|
const config = getConfig(),
|
||||||
manifest = getManifest(),
|
manifest = getManifest(),
|
||||||
rawURL = config.rawURL,
|
rawURL = config.rawURL,
|
||||||
repoURL = config.repository,
|
repoURL = config.repository,
|
||||||
manifestRoot = manifest!.root
|
manifestRoot = manifest!.root;
|
||||||
|
|
||||||
if (!config)
|
if (!config) cb(Error("foundryconfig.json not found"));
|
||||||
cb(Error("foundryconfig.json not found"))
|
|
||||||
if (manifest === null) {
|
if (manifest === null) {
|
||||||
cb(Error("Manifest JSON not found"))
|
cb(Error("Manifest JSON not found"));
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
if (!rawURL || !repoURL)
|
if (!rawURL || !repoURL) cb(Error("Repository URLs not configured in foundryconfig.json"));
|
||||||
cb(Error("Repository URLs not configured in foundryconfig.json"))
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const version = argv.update || argv.u
|
const version = argv.update || argv.u;
|
||||||
|
|
||||||
/* Update version */
|
/* Update version */
|
||||||
|
|
||||||
const versionMatch = /^(\d{1,}).(\d{1,}).(\d{1,})$/
|
const versionMatch = /^(\d{1,}).(\d{1,}).(\d{1,})$/;
|
||||||
const currentVersion = manifest!.file.version
|
const currentVersion = manifest!.file.version;
|
||||||
let targetVersion = ""
|
let targetVersion = "";
|
||||||
|
|
||||||
if (!version) {
|
if (!version) {
|
||||||
cb(Error("Missing version number"))
|
cb(Error("Missing version number"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (versionMatch.test(version)) {
|
if (versionMatch.test(version)) {
|
||||||
targetVersion = version
|
targetVersion = version;
|
||||||
} else {
|
} else {
|
||||||
targetVersion = currentVersion.replace(versionMatch, (substring: string, major: string, minor: string, patch: string) => {
|
targetVersion = currentVersion.replace(versionMatch, (substring: string, major: string, minor: string, patch: string) => {
|
||||||
console.log(substring, Number(major) + 1, Number(minor) + 1, Number(patch) + 1)
|
console.log(substring, Number(major) + 1, Number(minor) + 1, Number(patch) + 1);
|
||||||
if (version === "major") {
|
if (version === "major") {
|
||||||
return `${Number(major) + 1}.0.0`
|
return `${Number(major) + 1}.0.0`;
|
||||||
} else if (version === "minor") {
|
} else if (version === "minor") {
|
||||||
return `${major}.${Number(minor) + 1}.0`
|
return `${major}.${Number(minor) + 1}.0`;
|
||||||
} else if (version === "patch") {
|
} else if (version === "patch") {
|
||||||
return `${major}.${minor}.${Number(patch) + 1}`
|
return `${major}.${minor}.${Number(patch) + 1}`;
|
||||||
} else {
|
} else {
|
||||||
return ""
|
return "";
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (targetVersion === "") {
|
if (targetVersion === "") {
|
||||||
return cb(Error("Error: Incorrect version arguments."))
|
return cb(Error("Error: Incorrect version arguments."));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (targetVersion === currentVersion) {
|
if (targetVersion === currentVersion) {
|
||||||
return cb(Error("Error: Target version is identical to current version."))
|
return cb(Error("Error: Target version is identical to current version."));
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.ok(`Updating version number to '${targetVersion}'`)
|
Logger.ok(`Updating version number to '${targetVersion}'`);
|
||||||
|
|
||||||
packageJson.version = targetVersion
|
packageJson.version = targetVersion;
|
||||||
manifest.file.version = targetVersion
|
manifest.file.version = targetVersion;
|
||||||
|
|
||||||
/* Update URLs */
|
/* Update URLs */
|
||||||
|
|
||||||
const result = `${rawURL}/v${manifest.file.version}/${distPath}/${manifest.file.name}-v${manifest.file.version}.zip`
|
const result = `${repoURL}/releases/download`;
|
||||||
|
|
||||||
manifest.file.url = repoURL
|
manifest.file.url = repoURL;
|
||||||
manifest.file.manifest = `${rawURL}/master/${manifestRoot}/${manifest.name}`
|
manifest.file.manifest = `${result}/v${manifest.file.version}/${manifest.name}`;
|
||||||
manifest.file.download = result
|
manifest.file.download = `${result}/v${manifest.file.version}/${manifest.file.name}-v${manifest.file.version}.zip`;
|
||||||
|
|
||||||
const prettyProjectJson = stringify(manifest.file, {
|
const prettyProjectJson = stringify(manifest.file, {
|
||||||
maxLength: 35,
|
maxLength: 35,
|
||||||
indent: "\t",
|
indent: "\t",
|
||||||
})
|
});
|
||||||
|
|
||||||
fs.writeFileSync("package.json", JSON.stringify(packageJson, null, '\t'))
|
fs.writeFileSync("package.json", JSON.stringify(packageJson, null, "\t"));
|
||||||
fs.writeFileSync(path.join(manifest.root, manifest.name), prettyProjectJson, "utf8")
|
fs.writeFileSync(path.join(manifest.root, manifest.name), prettyProjectJson, "utf8");
|
||||||
|
|
||||||
return cb()
|
return cb();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return cb(err)
|
return cb(err);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const gitTaskManifest = (cb: gulp.TaskFunctionCallback) => {
|
const gitTaskManifest = (cb: gulp.TaskFunctionCallback) => {
|
||||||
const manifest = getManifest()
|
const manifest = getManifest();
|
||||||
if (!manifest)
|
if (!manifest) return cb(Error("could not load manifest."));
|
||||||
return cb(Error("could not load manifest."))
|
return gulp
|
||||||
|
.src([`package.json`, `source/system.json`])
|
||||||
return gulp.src([`package.json`, `source/module.json`])
|
|
||||||
.pipe(git.add({ args: "--no-all -f" }))
|
.pipe(git.add({ args: "--no-all -f" }))
|
||||||
.pipe(git.commit(`v${manifest.file.version}`, { args: "-a", disableAppendPaths: true }))
|
.pipe(git.commit(`v${manifest.file.version}`, { args: "-a", disableAppendPaths: true }));
|
||||||
}
|
};
|
||||||
|
|
||||||
const gitTaskBuild = (cb: gulp.TaskFunctionCallback) => {
|
const gitTaskBuild = (cb: gulp.TaskFunctionCallback) => {
|
||||||
const manifest = getManifest()
|
const manifest = getManifest();
|
||||||
if (!manifest)
|
if (!manifest) return cb(Error("could not load manifest."));
|
||||||
return cb(Error("could not load manifest."))
|
|
||||||
|
|
||||||
return gulp.src(`${distPath}/${manifest.file.name}-v${manifest.file.version}.zip`)
|
return gulp
|
||||||
.pipe(git.checkout(`v${manifest.file.version}`, { args: '-b' }))
|
.src(`${manifest.file.name}-v${manifest.file.version}.zip`)
|
||||||
|
.pipe(git.checkout(`v${manifest.file.version}`, { args: "-b" }))
|
||||||
.pipe(git.add({ args: "--no-all -f" }))
|
.pipe(git.add({ args: "--no-all -f" }))
|
||||||
.pipe(git.commit(`v${manifest.file.version}`, { args: "-a", disableAppendPaths: true }))
|
.pipe(git.commit(`v${manifest.file.version}`, { args: "-a", disableAppendPaths: true }));
|
||||||
}
|
};
|
||||||
|
|
||||||
const execBuild = gulp.parallel(buildTS, buildLess, buildPacks, copyFiles)
|
const execBuild = gulp.parallel(buildTS, buildLess, buildPacks, copyFiles);
|
||||||
|
|
||||||
exports.build = gulp.series(clean, execBuild, bundleModule)
|
exports.build = gulp.series(clean, execBuild, bundleModule);
|
||||||
exports.buildTarget = gulp.series(setTargetDir, clean, execBuild, bundleModule)
|
exports.buildTarget = gulp.series(setTargetDir, clean, execBuild, bundleModule);
|
||||||
exports.watch = buildWatch
|
exports.watch = buildWatch;
|
||||||
exports.watchTarget = gulp.series(setTargetDir, buildWatch)
|
exports.watchTarget = gulp.series(setTargetDir, buildWatch);
|
||||||
exports.clean = clean
|
exports.clean = clean;
|
||||||
exports.link = linkUserData
|
exports.link = linkUserData;
|
||||||
exports.package = packageBuild
|
exports.package = packageBuild;
|
||||||
exports.update = updateManifest
|
exports.update = updateManifest;
|
||||||
//exports.publish = gulp.series(clean, updateManifest, execBuild, bundleModule, packageBuild, gitTaskManifest, gitTaskBuild)
|
exports.publish = gulp.series(clean, updateManifest, execBuild, bundleModule, packageBuild, gitTaskManifest, gitTaskBuild);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue