@gapi/cli
Version:
Gapi command line interface
140 lines (139 loc) • 6.47 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BuildTask = void 0;
const core_1 = require("@rxdi/core");
const fs_1 = require("fs");
const util_1 = require("util");
const helpers_1 = require("../core/helpers");
const config_service_1 = require("../core/services/config.service");
const exec_service_1 = require("../core/services/exec.service");
const start_1 = require("./start");
let BuildTask = class BuildTask {
constructor() {
this.startTask = core_1.Container.get(start_1.StartTask);
this.configService = core_1.Container.get(config_service_1.ConfigService);
this.execService = core_1.Container.get(exec_service_1.ExecService);
}
run() {
return __awaiter(this, void 0, void 0, function* () {
const cwd = process.cwd();
const customPath = process.argv[4]
? process.argv[4].split('--path=')[1]
: null;
const customPathExists = yield (0, util_1.promisify)(fs_1.exists)(`${cwd}/${customPath}`);
const filePath = customPathExists
? `${cwd}/${customPath}`
: `${cwd}/src/main.ts`;
const globPaths = (0, helpers_1.nextOrDefault)('--glob', '')
.split(',')
.filter((i) => !!i)
.map((f) => `.${f}`);
if ((0, helpers_1.includes)('--ncc-only')) {
return this.compileToSingleJS(filePath);
}
if ((0, helpers_1.includes)('--pkg-only')) {
return this.compileToSingleJS(filePath);
}
if ((0, helpers_1.includes)('--single-executable')) {
if (!(yield this.isTsConfigPresent())) {
yield this.writeTsConfig();
}
yield this.compileToSingleJS(filePath);
yield this.compilePKG();
return;
}
yield this.startTask.prepareBundler(globPaths.length ? globPaths : filePath, {
original: this.configService.config.config.app.local,
schema: this.configService.config.config.schema,
});
});
}
compileToSingleJS(path) {
return this.execService.call(`npx ncc build ${path}`);
}
compilePKG() {
return __awaiter(this, void 0, void 0, function* () {
let packageName = (0, helpers_1.nextOrDefault)('--pkg-name', 'api');
if (yield (0, util_1.promisify)(fs_1.exists)('./package.json')) {
packageName =
JSON.parse(yield (0, util_1.promisify)(fs_1.readFile)('./package.json', { encoding: 'utf-8' })).name || packageName;
}
yield (0, util_1.promisify)(fs_1.writeFile)('./dist/package.json', JSON.stringify({
name: packageName,
bin: './index.js',
pkg: {
assets: [
'./protobuf/**/*',
'./protos/**/*',
'./protos.json',
'./operations.json',
...(0, helpers_1.nextOrDefault)('--assets', '')
.split(',')
.filter((v) => v),
],
},
}), { encoding: 'utf-8' });
yield this.execService.call('npx pkg .', { cwd: 'dist' });
});
}
isTsConfigPresent() {
return (0, util_1.promisify)(fs_1.exists)('./tsconfig.json');
}
writeTsConfig() {
return __awaiter(this, void 0, void 0, function* () {
yield (0, util_1.promisify)(fs_1.writeFile)('./tsconfig.json', JSON.stringify({
compilerOptions: {
module: 'commonjs',
target: 'es6',
declaration: true,
moduleResolution: 'node',
emitDecoratorMetadata: true,
experimentalDecorators: true,
removeComments: true,
allowSyntheticDefaultImports: true,
preserveConstEnums: true,
sourceMap: true,
strictNullChecks: false,
forceConsistentCasingInFileNames: true,
noFallthroughCasesInSwitch: true,
noImplicitAny: false,
noImplicitReturns: true,
noImplicitThis: false,
noUnusedLocals: true,
noUnusedParameters: false,
outDir: 'dist',
lib: [
'es2017',
'es2016',
'es2015',
'es6',
'dom',
'esnext.asynciterable',
],
typeRoots: ['node_modules/@types'],
},
include: ['./src/**/*'],
exclude: ['./node_modules', './src/**/*.spec.ts'],
}), { encoding: 'utf-8' });
});
}
};
exports.BuildTask = BuildTask;
exports.BuildTask = BuildTask = __decorate([
(0, core_1.Service)()
], BuildTask);