mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
build(): Refactor Gulpfile to TS
This commit is contained in:
8
tools/gulp/config.ts
Normal file
8
tools/gulp/config.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { getDirs } from './util/task-helpers';
|
||||
|
||||
// All paths are related to the base dir
|
||||
export const source = 'packages';
|
||||
export const integrationPath = 'integration';
|
||||
export const samplePath = 'sample';
|
||||
|
||||
export const packagePaths = getDirs(source);
|
||||
5
tools/gulp/gulpfile.ts
Normal file
5
tools/gulp/gulpfile.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import './tasks/copy-misc';
|
||||
import './tasks/clean';
|
||||
import './tasks/packages';
|
||||
import './tasks/move';
|
||||
import './tasks/samples';
|
||||
33
tools/gulp/tasks/clean.ts
Normal file
33
tools/gulp/tasks/clean.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { task, src, series } from 'gulp';
|
||||
import { source } from '../config';
|
||||
import * as clean from 'gulp-clean';
|
||||
import * as deleteEmpty from 'delete-empty';
|
||||
|
||||
/**
|
||||
* Cleans the build output assets from the packages folders
|
||||
*/
|
||||
function cleanOutput() {
|
||||
return src(
|
||||
[
|
||||
`${source}/**/*.js`,
|
||||
`${source}/**/*.d.ts`,
|
||||
`${source}/**/*.js.map`,
|
||||
`${source}/**/*.d.ts.map`,
|
||||
],
|
||||
{
|
||||
read: false,
|
||||
},
|
||||
).pipe(clean());
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans empty dirs
|
||||
*/
|
||||
function cleanDirs(done: () => void) {
|
||||
deleteEmpty.sync(`${source}/`);
|
||||
done();
|
||||
}
|
||||
|
||||
task('clean:output', cleanOutput);
|
||||
task('clean:dirs', cleanDirs);
|
||||
task('clean:bundle', series('clean:output', 'clean:dirs'));
|
||||
18
tools/gulp/tasks/copy-misc.ts
Normal file
18
tools/gulp/tasks/copy-misc.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { task, src, dest } from 'gulp';
|
||||
import { packagePaths } from '../config';
|
||||
|
||||
/**
|
||||
* Copies assets like Readme.md or LICENSE from the project base path
|
||||
* to all the packages.
|
||||
*/
|
||||
function copyMisc(): NodeJS.ReadWriteStream {
|
||||
const miscFiles = src(['Readme.md', 'LICENSE', '.npmignore']);
|
||||
// Since `dest()` does not take a string-array, we have to append it
|
||||
// ourselves
|
||||
return packagePaths.reduce(
|
||||
(stream, packagePath) => stream.pipe(dest(packagePath)),
|
||||
miscFiles,
|
||||
);
|
||||
}
|
||||
|
||||
task('copy-misc', copyMisc);
|
||||
23
tools/gulp/tasks/move.ts
Normal file
23
tools/gulp/tasks/move.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { task, src, dest } from 'gulp';
|
||||
import { getDirs } from '../util/task-helpers';
|
||||
import { samplePath, integrationPath } from '../config';
|
||||
import { join } from 'path';
|
||||
|
||||
/**
|
||||
* Moves the compiled nest files into the
|
||||
* `samples/*` and `integration/*` dirs.
|
||||
*/
|
||||
function move() {
|
||||
const samplesDirs = getDirs(samplePath);
|
||||
const integrationDirs = getDirs(integrationPath);
|
||||
const directories = samplesDirs.concat(integrationDirs);
|
||||
|
||||
const distFiles = src(['node_modules/@nestjs/**/*']);
|
||||
|
||||
return directories.reduce(
|
||||
(distFile, dir) => distFile.pipe(dest(join(dir, '/node_modules/@nestjs'))),
|
||||
distFiles,
|
||||
);
|
||||
}
|
||||
|
||||
task('move', move);
|
||||
78
tools/gulp/tasks/packages.ts
Normal file
78
tools/gulp/tasks/packages.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { source, packagePaths } from '../config';
|
||||
import { task, watch, series, dest } from 'gulp';
|
||||
import { createProject } from 'gulp-typescript';
|
||||
import * as sourcemaps from 'gulp-sourcemaps';
|
||||
import * as log from 'fancy-log';
|
||||
|
||||
// Has to be a hardcoded object due to build order
|
||||
const packages = {
|
||||
'common': createProject('packages/common/tsconfig.json'),
|
||||
'core': createProject('packages/core/tsconfig.json'),
|
||||
'microservices': createProject('packages/microservices/tsconfig.json'),
|
||||
'websockets': createProject('packages/websockets/tsconfig.json'),
|
||||
'testing': createProject('packages/testing/tsconfig.json'),
|
||||
'platform-express': createProject('packages/platform-express/tsconfig.json'),
|
||||
'platform-fastify': createProject('packages/platform-fastify/tsconfig.json'),
|
||||
'platform-socket.io': createProject(
|
||||
'packages/platform-socket.io/tsconfig.json',
|
||||
),
|
||||
'platform-ws': createProject('packages/platform-ws/tsconfig.json'),
|
||||
};
|
||||
|
||||
const modules = Object.keys(packages);
|
||||
|
||||
const distId = process.argv.indexOf('--dist');
|
||||
const dist = distId < 0 ? source : process.argv[distId + 1];
|
||||
|
||||
/**
|
||||
* Watches the packages/* folder and
|
||||
* builds the package on file change
|
||||
*/
|
||||
function defaultTask() {
|
||||
log.info('Watching files..');
|
||||
modules.forEach(packageName => {
|
||||
watch(
|
||||
[`${source}/${packageName}/**/*.ts`, `${source}/${packageName}/*.ts`],
|
||||
series(packageName),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the given package
|
||||
* @param packageName The name of the package
|
||||
*/
|
||||
function buildPackage(packageName: string) {
|
||||
return packages[packageName]
|
||||
.src()
|
||||
.pipe(packages[packageName]())
|
||||
.pipe(dest(`${dist}/${packageName}`));
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the given package and adds sourcemaps
|
||||
* @param packageName The name of the package
|
||||
*/
|
||||
function buildPackageDev(packageName: string) {
|
||||
return packages[packageName]
|
||||
.src()
|
||||
.pipe(sourcemaps.init())
|
||||
.pipe(packages[packageName]())
|
||||
.pipe(
|
||||
sourcemaps.mapSources(
|
||||
(sourcePath: string) => './' + sourcePath.split('/').pop(),
|
||||
),
|
||||
)
|
||||
.pipe(sourcemaps.write('.', {}))
|
||||
.pipe(dest(`${dist}/${packageName}`));
|
||||
}
|
||||
|
||||
modules.forEach(packageName => {
|
||||
task(packageName, () => buildPackage(packageName));
|
||||
task(`${packageName}:dev`, () => buildPackageDev(packageName));
|
||||
});
|
||||
|
||||
task('common:dev', series(modules.map(packageName => `${packageName}:dev`)));
|
||||
task('build', series(modules));
|
||||
task('build:dev', series('common:dev'));
|
||||
task('default', defaultTask);
|
||||
58
tools/gulp/tasks/samples.ts
Normal file
58
tools/gulp/tasks/samples.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { resolve } from 'path';
|
||||
import { promisify } from 'util';
|
||||
import * as childProcess from 'child_process';
|
||||
|
||||
import { task } from 'gulp';
|
||||
|
||||
import * as log from 'fancy-log';
|
||||
import * as clc from 'cli-color';
|
||||
|
||||
import { getDirs } from '../util/task-helpers';
|
||||
import { samplePath } from '../config';
|
||||
|
||||
const exec = promisify(childProcess.exec);
|
||||
|
||||
/**
|
||||
* Installs all the npm packages in the
|
||||
* `samples/*` folder
|
||||
*/
|
||||
async function installSamples() {
|
||||
const directories = getDirs(samplePath);
|
||||
|
||||
for await (const dir of directories) {
|
||||
const dirName = dir.replace(resolve(__dirname, '../../../'), '');
|
||||
log.info(`Installing dependencies of ${clc.magenta(dirName)}`);
|
||||
try {
|
||||
await exec(`npm install --no-shrinkwrap --prefix ${dir}`);
|
||||
log.info(`Finished installing ${clc.magenta(dirName)}`);
|
||||
} catch (err) {
|
||||
log.error(`Failed installing dependencies of ${dirName}`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds all the `samples/*`
|
||||
*/
|
||||
async function buildSamples() {
|
||||
const directories = getDirs(samplePath);
|
||||
|
||||
for await (const dir of directories) {
|
||||
const dirName = dir.replace(__dirname, '');
|
||||
log.info(`Building ${clc.magenta(dirName)}`);
|
||||
try {
|
||||
await exec(`npm run build --prefix ${dir}`);
|
||||
log.info(`Finished building ${clc.magenta(dirName)}`);
|
||||
} catch (err) {
|
||||
log.error(`Failed building ${clc.magenta(dirName)}:`);
|
||||
if (err.stdout) {
|
||||
log.error(err.stdout);
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
task('install:samples', async () => await installSamples());
|
||||
task('build:samples', async () => await buildSamples());
|
||||
25
tools/gulp/tsconfig.json
Normal file
25
tools/gulp/tsconfig.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"noUnusedParameters": false,
|
||||
"noUnusedLocals": false,
|
||||
"lib": ["es2015", "dom", "es2016.array.include"],
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"outDir": "../../dist/tools/gulp",
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"noImplicitThis": true,
|
||||
"noEmitOnError": true,
|
||||
"noImplicitAny": false,
|
||||
"target": "es5",
|
||||
"types": [
|
||||
"node"
|
||||
],
|
||||
"typeRoots": ["./typings", "../../node_modules/@types/"],
|
||||
"baseUrl": ".",
|
||||
},
|
||||
"files": [
|
||||
"gulpfile.ts"
|
||||
]
|
||||
}
|
||||
14
tools/gulp/util/task-helpers.ts
Normal file
14
tools/gulp/util/task-helpers.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { readdirSync, statSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
function isDirectory(path: string) {
|
||||
return statSync(path).isDirectory();
|
||||
}
|
||||
|
||||
export function getFolders(dir: string) {
|
||||
return readdirSync(dir).filter(file => isDirectory(join(dir, file)));
|
||||
}
|
||||
|
||||
export function getDirs(base: string) {
|
||||
return getFolders(base).map(path => `${base}/${path}`);
|
||||
}
|
||||
Reference in New Issue
Block a user