UNPKG

@gluestack-v2/framework-cli

Version:

Gluestack V2 Framework CLI

261 lines (260 loc) 12.4 kB
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()); }); }; (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "path", "../constants/gluestack.v2", "../constants/gluestack.v2", "@gluestack/helpers", "../helpers/file", "child_process", "../helpers/print", "../helpers/execute"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = require("path"); const gluestack_v2_1 = require("../constants/gluestack.v2"); const gluestack_v2_2 = require("../constants/gluestack.v2"); const helpers_1 = require("@gluestack/helpers"); const file_1 = require("../helpers/file"); const child_process_1 = require("child_process"); const print_1 = require("../helpers/print"); const execute_1 = require("../helpers/execute"); class BaseGluestackPluginInstance { constructor(app, callerPlugin, name, gluePluginStore, installationPath) { this.isOfTypeInstance = false; this.app = app; this.name = name; this.callerPlugin = callerPlugin; this.gluePluginStore = gluePluginStore; this.installationPath = installationPath; } build() { return __awaiter(this, void 0, void 0, function* () { }); } prepare() { return __awaiter(this, void 0, void 0, function* () { if (this._instanceType === 'service') { yield (0, execute_1.execute)('sh', [ '-c', `cd ${(0, path_1.join)(this._destinationPath, '..')} && npm run install:all`, ], { stdio: 'inherit' }); } if (this._instanceType === 'package') { yield (0, execute_1.execute)('sh', [ '-c', `cd ${this._destinationPath} && npm i && npm run build`, ], { stdio: 'inherit', }); } }); } getName() { return this.name; } getCallerPlugin() { return this.callerPlugin; } getDestinationPath() { return (0, path_1.join)(process.cwd(), gluestack_v2_1.GLUE_GENERATED_SERVICES_PATH, this.getName(), 'src', this.getName()); } getPluginEnvironment() { // @ts-ignore return this.callerPlugin.getPluginEnvironment(); } getSourcePath() { return (0, path_1.join)(process.cwd(), this.getPluginEnvironment(), this.getName()); } getWorkspacePath() { return (0, path_1.join)(process.cwd(), gluestack_v2_1.GLUE_GENERATED_SERVICES_PATH, this.getName(), 'src'); } get _workspacePath() { return this.getWorkspacePath(); } get _sourcePath() { return this.getSourcePath(); } get _destinationPath() { return this.getDestinationPath(); } updateSourcePackageJSON() { return __awaiter(this, void 0, void 0, function* () { // update package.json'S name index with the new instance name this.app.updateNameInPackageJSON(this._sourcePath, this.getName()); }); } updateDestinationPackageJSON() { return __awaiter(this, void 0, void 0, function* () { // update package.json'S name index with the new instance name this.app.updateNameInPackageJSON(this._destinationPath, this.getName()); }); } filterEnvData(envPath, pluginEnv) { return __awaiter(this, void 0, void 0, function* () { const envData = yield (0, file_1.readEnvFile)(envPath); const envDataArray = envData ? envData.split('\n') : []; const envObjectsArray = envDataArray.map((envDataItem) => { if (!envDataItem) return; return envDataItem.split('='); }); const envDataFiltered = envObjectsArray.filter((envDataItem) => { if (envDataItem) { if (pluginEnv === 'client') { return !envDataItem[0].startsWith('SERVER_'); } else { return !envDataItem[0].startsWith('CLIENT_'); } } }); const filteredObject = Object.fromEntries(envDataFiltered); return filteredObject; }); } generateEnvDataFromObject(envObject) { const envDataArray = Object.entries(envObject); const envDataString = envDataArray .map((envDataItem) => { return `${envDataItem[0]}=${envDataItem[1]}`; }) .join('\n'); return envDataString; } generateEnvFiles() { return __awaiter(this, void 0, void 0, function* () { const rootEnvPath = (0, path_1.join)(process.cwd(), '.env'); //@ts-ignore const pluginEnv = this.callerPlugin.pluginEnvironment; const filteredEnv = yield this.filterEnvData(rootEnvPath, pluginEnv); yield (0, file_1.writeFile)((0, path_1.join)(this._workspacePath, '.env'), this.generateEnvDataFromObject(filteredEnv)); }); } updateRootPackageJSONWithSourcePath() { return __awaiter(this, void 0, void 0, function* () { // update root package.json's workspaces with the new instance name const rootPackage = `${process.cwd()}/package.json`; yield helpers_1.Workspaces.append(rootPackage, (0, path_1.relative)(process.cwd(), this._sourcePath)); }); } updateRootPackageJSONWithDestinationPath() { return __awaiter(this, void 0, void 0, function* () { const rootPackage = `${process.cwd()}/package.json`; yield helpers_1.Workspaces.append(rootPackage, (0, path_1.relative)(process.cwd(), this._destinationPath)); }); } addServiceToWorkspaces(workspacePath) { return __awaiter(this, void 0, void 0, function* () { const rootPackage = `${process.cwd()}/package.json`; yield helpers_1.Workspaces.append(rootPackage, (0, path_1.relative)(process.cwd(), workspacePath)); }); } buildPackage(packagePath) { return __awaiter(this, void 0, void 0, function* () { const child = (0, child_process_1.spawn)('sh', ['-c', `cd ${packagePath} && npm i && npm run build`], { stdio: 'inherit', }); child.on('exit', () => (0, print_1.info)('done')); child.on('close', (code) => code === 0 ? (0, print_1.info)('done') : (0, print_1.error)('failed')); }); } updateWorkspacePackageJSON() { return __awaiter(this, void 0, void 0, function* () { // // add package.json with workspaces const packageFile = (0, path_1.join)(this._workspacePath, 'package.json'); const packageContent = { name: this.getName(), private: true, workspaces: [this.getName(), 'packages/**'], scripts: { 'install:all': 'npm install --workspaces --if-present', build: 'npm run build --workspaces --if-present', dev: 'npm run dev --workspace @project/' + this.getName(), }, }; yield (0, file_1.writeFile)(packageFile, JSON.stringify(packageContent, null, 2)); }); } boltInit() { return __awaiter(this, void 0, void 0, function* () { // bolt init and bolt service add in the services folder if (!(0, file_1.fileExistsSync)((0, path_1.join)(gluestack_v2_2.BOLT_SERVICES_PATH, 'bolt.yaml'))) { const boltInit = (0, child_process_1.spawnSync)('sh', [ '-c', `cd ${gluestack_v2_2.BOLT_SERVICES_PATH} && bolt init`, ]); if (boltInit.status !== 0) { console.error(`Command failed with code ${boltInit.status}`); } // eslint-disable-next-line no-console console.log(boltInit.stdout.toString()); console.error(boltInit.stderr.toString()); } const boltAddService = (0, child_process_1.spawnSync)('sh', [ '-c', `cd ${gluestack_v2_1.GLUE_GENERATED_SERVICES_PATH} && bolt service:add ${this.getName()} ./${this.getName()}/src/${this.getName()}`, ]); if (boltAddService.status !== 0) { console.error(`Command failed with code ${boltAddService.status}`); } // eslint-disable-next-line no-console console.log(boltAddService.stdout.toString()); console.error(boltAddService.stderr.toString()); }); } boltUp(servicePlatform) { return __awaiter(this, void 0, void 0, function* () { // bolt init and bolt service add in the services folder const boltInit = (0, child_process_1.spawnSync)('sh', [ '-c', `cd ${gluestack_v2_1.GLUE_GENERATED_SERVICES_PATH} && bolt service:up ${this.getName()} --service-runner ${servicePlatform}`, ]); if (boltInit.status !== 0) { console.error(`Command failed with code ${boltInit.status}`); } // eslint-disable-next-line no-console console.log(boltInit.stdout.toString()); console.error(boltInit.stderr.toString()); }); } buildBeforeWatch() { return __awaiter(this, void 0, void 0, function* () { if (!(yield (0, file_1.fileExistsSync)(this._destinationPath))) { try { yield this.build(); } catch (error) { // eslint-disable-next-line no-console console.log('>> Instance does not exits:', this.getName()); return; } } }); } watch(callback) { return __awaiter(this, void 0, void 0, function* () { yield this.buildBeforeWatch(); this.app.watch(this._sourcePath, '', (event, path) => __awaiter(this, void 0, void 0, function* () { if (callback) { callback(event, path); } })); }); } get _instanceType() { return this._destinationPath.includes('packages') ? 'package' : this._destinationPath.includes('services') ? 'service' : 'none'; } } exports.default = BaseGluestackPluginInstance; });