UNPKG

1.15 kBPlain TextView Raw
1import { existsSync, readFileSync, writeFileSync } from 'fs';
2
3export function wrapPilet(file: string, prName: string) {
4 if (existsSync(file)) {
5 const template = readFileSync(file, 'utf8');
6 const content = template.replace(
7 /^\!function\s?\(e,\s?t\)\s?\{/m,
8 `!function(e,t){function define(d,k){(typeof document!=='undefined')&&(document.currentScript.app=k.apply(null,d.map(window.${prName})));}define.amd=!0;`,
9 );
10
11 writeFileSync(file, content);
12 }
13}
14
15export function getVariables(piletPkg: any, env: string): Record<string, string> {
16 return {
17 NODE_ENV: env,
18 BUILD_TIME: new Date().toDateString(),
19 BUILD_TIME_FULL: new Date().toISOString(),
20 BUILD_PCKG_VERSION: piletPkg.version,
21 BUILD_PCKG_NAME: piletPkg.name,
22 };
23}
24
25export function setEnvironment(variables: Record<string, string>) {
26 Object.keys(variables).forEach((key) => (process.env[key] = variables[key]));
27}
28
29export function getDefineVariables(variables: Record<string, string>) {
30 return Object.entries(variables).reduce((obj, [name, value]) => {
31 obj[`process.env.${name}`] = JSON.stringify(value);
32 return obj;
33 }, {});
34}