const writePackageJson = async (json) => fs.writeJson('package.json', json, { spaces: 2, EOL: os.EOL })
async writeConfig(config) { const { cubeCloudConfigPath, configFile } = this.configFile(); await fs.mkdirp(cubeCloudConfigPath); await fs.writeJson(configFile, config); }
async persistSources(sourceContainer, packageVersions) { const sources = sourceContainer.outputSources(); await Promise.all(sources.map((file) => fs.outputFile(path.join(this.appPath, file.fileName), file.content))); const packageJson = fs.readJsonSync(path.join(this.appPath, 'package.json')); packageJson.cubejsTemplates = { ...packageJson.cubejsTemplates, ...packageVersions, }; await fs.writeJson(path.join(this.appPath, 'package.json'), packageJson, { spaces: 2, }); }
async writeDotCubeCloud(config) { await fs.writeJson(this.dotCubeCloudFile(), config); }
save(json, next) { if (!json || !_.isObject(json) || _.isNull(json) || !_.keys(json).length) { throw new Error('Invalid JSON was passed to Builder.'); } Fs.writeJson('./config/core.json', json, { spaces: 2 }, err => { if (!err) Cache.put('config', json); return next(err); }); }
async function set (key, value) { await ensureConfig() if (value == null) { delete config[key] } else { config[key] = value } await fse.writeJson(configFile, config) }
async writeAndroidPluginFile(root, path, config) { await fse.ensureDir(root) await fse.ensureFile(path) await fse.writeJson(path, config, { spaces: '\t' }) }
/** * @param {object} data * @param {string} data.filePath Absolute path to file in which config will be written * @returns {Promise<void>} */ initProjectConfig(data) { return Promise.resolve() .then(() => fs.pathExists(data.filePath)) .then(configExists => Util.stop(configExists, `'${Constants.ConfigFileName}' already exists.`)) .then(() => { const projectConfig = { rammyVersion: Util.getPackageVersion(), modules: {}, }; return fs.writeJson(data.filePath, projectConfig, {spaces: 2}); }); }
async uninstallInPackage(dir, pluginName) { const packageJsonPath = path.join(dir, 'package.json') // Update package.json if (fse.existsSync(packageJsonPath)) { const packageJson = require(packageJsonPath) if (packageJson.dependencies[pluginName]) { delete packageJson.dependencies[pluginName] } fse.writeJson(packageJsonPath, packageJson, { spaces: '\t' }) } this.emit(LOGLEVEL.INFO, `Update plugins.json...`) // Update plugin.json in the project. this.pluginConfigs = utils.updatePluginConfigs(this.pluginConfigs, pluginName, {}, 'web') await utils.writePluginFile(this.config.rootPath, this.pluginConfigPath, this.pluginConfigs) this.emit(LOGLEVEL.INFO, `Building plugins...`) await utils.buildJS('build:plugin', true) this.emit(LOGLEVEL.SUCCESS, `Building plugins successful.`) }
modify(object, next) { if (!_.isObject(object)) return next(new Error('Function expects an object to be passed.')); const deepExtend = extendify({ inPlace: false, arrays: 'replace', }); const modifiedJson = deepExtend(Cache.get('config'), object); Fs.writeJson('./config/core.json', modifiedJson, { spaces: 2 }, err => { if (err) return next(err); Cache.put('config', modifiedJson); return next(); }); }
async installInPackage(dir, pluginName, version, option) { const packageFilePath = path.join(dir, 'package.json') this.emit(LOGLEVEL.LOG, 'Downloading plugin...') if (fse.existsSync(packageFilePath)) { const pkg = require(packageFilePath) pkg.dependencies[pluginName] = version fse.writeJson(packageFilePath, pkg, { spaces: '\t' }) } await utils.installNpmPackage() const browserPluginName = option.web && option.web.name ? option.web.name : pluginName if (option.web) { this.emit(LOGLEVEL.LOG, `Update plugins.json...`) // Update plugin.json in the project. this.pluginConfigs = utils.updatePluginConfigs(this.pluginConfigs, browserPluginName, option, 'web') await utils.writePluginFile(this.config.rootPath, this.pluginConfigPath, this.pluginConfigs) this.emit(LOGLEVEL.LOG, `Building plugins...`) try { await utils.buildJS('build:plugin', true) this.emit(LOGLEVEL.SUCCESS, `Building plugins successful.`) } catch (error) { this.emit(LOGLEVEL.ERROR, error) } } }
async installForWeb(plugins) { if (!Array.isArray(plugins) || plugins.length <= 0) { return } const packageJsonFile = path.join(this.config.root, 'package.json') let packageJson = JSON.parse(fse.readFileSync(packageJsonFile)) plugins.forEach(plugin => { packageJson['dependencies'][plugin.name] = plugin.version }) packageJson = utils.sortDependencies(packageJson) fse.writeJson(packageJsonFile, packageJson, { spaces: '\t' }) this.emit(LOGLEVEL.LOG, `Downloading plugins...`) await utils.installNpmPackage() this.emit(LOGLEVEL.LOG, `Building plugins...`) try { await utils.buildJS('build:plugin', true) this.emit(LOGLEVEL.SUCCESS, `Building plugins successful.`) } catch (error) { this.emit(LOGLEVEL.ERROR, error) } }
/** * Writes out config data to the config file * * @returns {Promise<void>} */ writeOut() { return Promise.resolve() .then(() => fs.writeJson(this.rammyrcPath, this.configData, {spaces: 2})); }
/** * @param {object} data * @param {string} data.filePath Absolute path to file in which config will be written * @param {string} data.moduleName * @returns {Promise<void>} */ initModuleConfig(data) { return Promise.resolve() .then(() => fs.pathExists(data.filePath)) .then(configExists => Util.stop(configExists, `'${Constants.ConfigFileName}' already exists.`)) .then(() => { const moduleConfig = { rammyVersion: Util.getPackageVersion(), name: data.moduleName, module: true, templates: {}, snippets: {}, }; return fs.writeJson(data.filePath, moduleConfig, {spaces: 2}); }); }
async writePluginFile(root, path, config) { await fse.ensureDir(root) await fse.ensureFile(path) await fse.writeJson(path, config, { spaces: '\t' }) }