-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(build): unify the windows and linux script (#1116)
- Loading branch information
Showing
11 changed files
with
822 additions
and
40 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { resolve } from 'path'; | ||
|
||
type FolderCaterogy = 'dist' | 'root' | 'src' | 'nodeModules'; | ||
|
||
type IPaths = Record<FolderCaterogy, string> & { [index: string]: string }; | ||
|
||
const ROOT_LEVEL = '../../../'; | ||
const ROOT = resolve(__dirname, ROOT_LEVEL); | ||
|
||
export const resolveRoot = (relativePath: string): string => { | ||
return resolve(ROOT, relativePath); | ||
}; | ||
|
||
export const PATHS: IPaths = { | ||
dist: resolveRoot('dist'), | ||
nodeModules: resolveRoot('node_modules'), | ||
src: resolveRoot('src'), | ||
root: ROOT | ||
}; | ||
|
||
export const resolvePackage = (name: string, ...paths: string[]): string => { | ||
return resolve(PATHS.packages, name, ...paths); | ||
}; | ||
|
||
export const resolveDist = (name: string): string => { | ||
return resolve(PATHS.dist, name); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { readFileSync } from 'fs'; | ||
import { writeFile } from 'fs/promises'; | ||
|
||
import { resolveRoot } from '../../config/paths'; | ||
import * as log from '../../utils/log'; | ||
|
||
export async function setVersionFile(version: string) { | ||
const filePath = resolveRoot('src/environments/version.ts'); | ||
|
||
let body = readFileSync(filePath, 'utf-8'); | ||
body = body.replace(/app: '[A-Za-z0-9\.\-]+'/g, `app: '${version}'`); | ||
body = body.replace( | ||
/releaseDateApp: [0-9]+/g, | ||
`releaseDateApp: ${Date.now()}` | ||
); | ||
|
||
await writeFile(filePath, body, { flag: 'w' }); | ||
log.success('The version file has been written'); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { setVersionFile } from './core/utils/version.utils'; | ||
import { writeFile2 } from './utils/file-system.utils'; | ||
import * as log from './utils/log'; | ||
import { getDuration } from './utils/performance.utils'; | ||
|
||
const baseCmdName = 'Application release'; | ||
|
||
log.startMsg(baseCmdName); | ||
(async () => { | ||
const startTime = performance.now(); | ||
try { | ||
const [_nodePath, _scriptPath, argVersion, type] = process.argv; | ||
const version = argVersion ?? process.env.npm_new_version; | ||
|
||
if (!version) { | ||
throw new Error('No version detected'); | ||
} | ||
|
||
log.info('Create the version.ts file'); | ||
await setVersionFile(version); | ||
|
||
await deployGithubPage(version); | ||
|
||
log.success(`Release version ${version}`); | ||
|
||
const duration = getDuration(startTime); | ||
log.info(`${baseCmdName} excuted in ${duration}.`); | ||
} catch (err: any) { | ||
log.error(`The release failed with: ${err?.message}`); | ||
process.exit(1); | ||
} | ||
})(); | ||
|
||
async function deployGithubPage(version: string): Promise<void> { | ||
const startTime = performance.now(); | ||
const { $ } = await import('execa'); | ||
const $$ = $({ stdio: 'inherit' }); | ||
|
||
log.info('Build the app for Github'); | ||
await $$`npm run build.github`; | ||
await writeFile2( | ||
'dist/ghpages/_config.yml', | ||
"include: ['_default.json', '_contexts.json', '_base.json']", | ||
false | ||
); | ||
|
||
await $$`npx ngh --dir=dist/ghpages --no-silent=false --message=${version}`; | ||
|
||
const duration = getDuration(startTime); | ||
log.success( | ||
`Deploy the app v${version} on Github Page, excuted in ${duration}.` | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Source: https://github.com/ng-packagr/ng-packagr/blob/ee4fd635a626e1ee2266b05cb572002bb09b4849/src/lib/utils/color.ts | ||
*/ | ||
import * as ansiColors from 'ansi-colors'; | ||
import { WriteStream } from 'tty'; | ||
|
||
type AnsiColors = typeof ansiColors; | ||
|
||
function supportColor(): boolean { | ||
if (process.env.FORCE_COLOR !== undefined) { | ||
// 2 colors: FORCE_COLOR = 0 (Disables colors), depth 1 | ||
// 16 colors: FORCE_COLOR = 1, depth 4 | ||
// 256 colors: FORCE_COLOR = 2, depth 8 | ||
// 16,777,216 colors: FORCE_COLOR = 3, depth 16 | ||
// See: https://nodejs.org/dist/latest-v12.x/docs/api/tty.html#tty_writestream_getcolordepth_env | ||
// and https://github.com/nodejs/node/blob/b9f36062d7b5c5039498e98d2f2c180dca2a7065/lib/internal/tty.js#L106; | ||
switch (process.env.FORCE_COLOR) { | ||
case '': | ||
case 'true': | ||
case '1': | ||
case '2': | ||
case '3': | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
if (process.stdout instanceof WriteStream) { | ||
return process.stdout.getColorDepth() > 1; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
// Create a separate instance to prevent unintended global changes to the color configuration | ||
// Create function is not defined in the typings. See: https://github.com/doowb/ansi-colors/pull/44 | ||
const colors = ( | ||
ansiColors as AnsiColors & { create: () => AnsiColors } | ||
).create(); | ||
colors.enabled = supportColor(); | ||
|
||
export { colors }; |
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { existsSync, readFileSync } from 'fs'; | ||
import { | ||
copyFile as fsCopyFile, | ||
mkdir, | ||
readFile, | ||
writeFile | ||
} from 'fs/promises'; | ||
import { normalize, sep } from 'path'; | ||
|
||
export const BUFFER_ENCODING: BufferEncoding = 'utf-8'; | ||
|
||
export async function readFileContent<T>(path: string): Promise<T> { | ||
const body = await readFile(path, BUFFER_ENCODING); | ||
return JSON.parse(body) as T; | ||
} | ||
|
||
export function readFileContentSync<T>(path: string): T { | ||
const body = readFileSync(path, BUFFER_ENCODING); | ||
return JSON.parse(body) as T; | ||
} | ||
|
||
export async function createFile( | ||
fileName: string, | ||
dest: string, | ||
body: string | ||
): Promise<void> { | ||
const path = `${dest}/${fileName}`; | ||
try { | ||
await writeFile(path, body, BUFFER_ENCODING); | ||
} catch (err: any) { | ||
if (err.code === 'ENOENT') { | ||
await createFolderRecursively(dest); | ||
await writeFile(path, body, BUFFER_ENCODING); | ||
} | ||
} | ||
} | ||
|
||
export async function copyFile(src: string, dest: string): Promise<void> { | ||
try { | ||
await fsCopyFile(src, dest); | ||
} catch (err: any) { | ||
if (err.code === 'ENOENT') { | ||
await createPreviousFolder(dest); | ||
await fsCopyFile(src, dest); | ||
} | ||
} | ||
} | ||
|
||
export async function createFolderRecursively(dest: string): Promise<void> { | ||
try { | ||
await mkdir(dest); | ||
} catch (err: any) { | ||
if (err.code === 'ENOENT') { | ||
await createPreviousFolder(dest); | ||
await createFolderRecursively(dest); | ||
} | ||
} | ||
} | ||
|
||
async function createPreviousFolder(dest: string): Promise<void> { | ||
const folders = normalize(dest).split(sep); | ||
folders.pop(); | ||
await createFolderRecursively(folders.join(sep)); | ||
} | ||
|
||
export function pathExist(path: string): boolean { | ||
return existsSync(path); | ||
} | ||
|
||
export function writeFile2( | ||
path: string, | ||
body: object | string, | ||
endLineBreak = true | ||
): Promise<void> { | ||
let formattedBody = | ||
typeof body === 'string' ? body : JSON.stringify(body, null, 2); | ||
if (endLineBreak) { | ||
formattedBody = formattedBody.concat('\n'); | ||
} | ||
return writeFile(path, formattedBody, BUFFER_ENCODING); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Source: https://github.com/ng-packagr/ng-packagr/blob/ee4fd635a626e1ee2266b05cb572002bb09b4849/src/lib/utils/log.ts | ||
*/ | ||
/* eslint-disable no-console */ | ||
import { colors } from './color'; | ||
|
||
export const error = (err: string | Error) => { | ||
if (err instanceof Error) { | ||
console.error(colors.red('ERROR: ' + err.message)); | ||
|
||
if (process.env.DEBUG) { | ||
console.error(colors.red(err.stack ?? '') + '\n'); | ||
} | ||
} else { | ||
console.error(colors.red(err)); | ||
} | ||
}; | ||
|
||
export const warn = (msg: string) => { | ||
console.warn(colors.yellow('WARNING: ' + msg)); | ||
}; | ||
|
||
export const success = (msg: string) => { | ||
console.log(colors.green(msg)); | ||
}; | ||
|
||
export const info = (msg: string) => { | ||
console.log(colors.blue(msg)); | ||
}; | ||
|
||
export const msg = (msg: string) => { | ||
console.log(colors.white(msg)); | ||
}; | ||
|
||
export const debug = (msg: string) => { | ||
if (process.env.DEBUG) { | ||
console.log(colors.inverse.cyan(`[debug] ${msg}`)); | ||
} | ||
}; | ||
|
||
export const startMsg = (message: string) => { | ||
msg( | ||
'\n------------------------------------------------------------------------------' | ||
); | ||
msg(message); | ||
msg( | ||
'------------------------------------------------------------------------------' | ||
); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** Duration in ms */ | ||
export function getDuration(startTime: number): string { | ||
const duration = Math.round(performance.now() - startTime); | ||
return `${duration}ms`; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": ["es2022"], | ||
"module": "NodeNext", | ||
"moduleResolution": "NodeNext", | ||
"target": "es2022", | ||
"rootDir": "./", | ||
"isolatedModules": true, | ||
"noImplicitAny": true, | ||
"sourceMap": true, | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true | ||
}, | ||
"exclude": ["node_modules"], | ||
"ts-node": { | ||
"swc": true | ||
} | ||
} |
Oops, something went wrong.