Skip to content

Instantly share code, notes, and snippets.

@DJ1TJOO
Created January 15, 2022 12:20
Show Gist options
  • Save DJ1TJOO/bea475578c3aa68de9f760f7fce0edb9 to your computer and use it in GitHub Desktop.
Save DJ1TJOO/bea475578c3aa68de9f760f7fce0edb9 to your computer and use it in GitHub Desktop.
function gulptasksMod($, gulp, buildFolder, browserSync) {
gulp.task("mod.declarations", async cb => {
exec(
"tsc src/js/application.js --declaration --allowJs --emitDeclarationOnly --skipLibCheck --out types.js",
{
cwd: "../",
},
(error, stdout, sterr) => {
if (error) {
console.log(stdout);
console.log(sterr);
throw error;
}
const stream = gulp
.src("../types.d.ts")
.pipe(
$.replace(
/declare module "([^]*?)"/gms,
(matched, moduleName) => `declare module "shapez/${moduleName}"`
)
)
.pipe(
$.replace(/import\("([^]*?)"\)/gms, (matched, moduleName, offset, string) => {
moduleName = moduleName.replace(".js", "");
if (moduleName.startsWith(".")) {
const closest = getClosest(string, offset, /declare module "([^]*?)"/gms);
const parent = path.dirname(
closest["0"].replace('declare module "', "").replace('"', "")
);
const module = path.join(parent, moduleName).replace(/\\/g, "/");
return `import("${module}")`;
} else {
return `import("shapez/${moduleName}")`;
}
})
)
.pipe(
$.replace(
/import {([^]*?)} from "([^]*?)";/gms,
(matched, imports, moduleName) =>
`import {${imports}} from "shapez/${moduleName
.replace(/\.\.\//gms, "")
.replace(".js", "")}"`
)
)
.pipe($.replace(/var/gms, "let"))
.pipe($.replace(/declare (function|const|let)[^]*?;/gms, ""))
.pipe(
$.footer(`declare const CSS: string;
declare const ATLASES: {
hq: {
src: string;
atlasData: import("shapez/core/loader").AtlasDefinition;
};
mq: {
src: string;
atlasData: import("shapez/core/loader").AtlasDefinition;
};
lq: {
src: string;
atlasData: import("shapez/core/loader").AtlasDefinition;
};
};
declare const TRANSLATIONS: {
[x: string]: object
};`),
declare const THEMES: {
[x: string]: object
};`)
)
.pipe(
$.prettier({
editorconfig: true,
})
)
.pipe(gulp.dest("../build/"));
stream.on("end", () => {
fs.unlinkSync("../types.d.ts");
cb();
});
stream.on("error", () => {
fs.unlinkSync("../types.d.ts");
cb();
});
}
);
});
}
module.exports = {
gulptasksMod,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment