@google/dscc-gen
Version:
Create component & connector projects with sane defaults.
209 lines • 9.79 kB
JavaScript
;
/**
* @license
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFromTemplate = void 0;
const execa = require("execa");
const fs = require("mz/fs");
const path = require("path");
const terminal_link_1 = require("terminal-link");
const constants_1 = require("../constants");
const files = require("../files");
const types_1 = require("../types");
const util = require("../util");
const util_1 = require("../util");
const appsscript = require("./appsscript");
const validation = require("./validation");
const OAUTH2_LIBRARY = {
userSymbol: 'OAuth2',
libraryId: '1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF',
version: '33',
};
const getTemplates = (config) => {
return [
{ match: /{{MANIFEST_NAME}}/, replace: config.projectName },
{ match: /{{MANIFEST_LOGO_URL}}/, replace: config.manifestLogoUrl },
{ match: /{{MANIFEST_COMPANY}}/, replace: config.manifestCompany },
{ match: /{{MANIFEST_COMPANY_URL}}/, replace: config.manifestCompanyUrl },
{ match: /{{MANIFEST_ADDON_URL}}/, replace: config.manifestAddonUrl },
{ match: /{{MANIFEST_SUPPORT_URL}}/, replace: config.manifestSupportUrl },
{ match: /{{MANIFEST_DESCRIPTION}}/, replace: config.manifestDescription },
{
match: /{{MANIFEST_SOURCES}}/,
replace: `[${config
.manifestSources.split(',')
.map((a) => `"${a}"`)
.join(',')}]`,
},
];
};
const ensureAuthenticated = async (execOptions) => {
const authenticated = await util.spinnify('Ensuring clasp is authenticated...', async () => {
return validation.claspAuthenticated();
});
if (authenticated === false) {
const infoText = util_1.format.yellow('Clasp must be globally authenticated for dscc-gen.');
const claspLogin = util_1.format.green('npx @google/clasp login');
console.log(`${infoText}\nrunning ${claspLogin} ...\n`);
await execa('npx', ['@google/clasp', 'login'], Object.assign({}, execOptions, { stdio: 'inherit' }));
}
};
const installDependencies = async (projectPath, config) => {
return util.spinnify('Installing project dependencies...', async () => util.npmInstall(projectPath, config));
};
const createAppsScriptProject = async (projectPath, projectName, execOptions, config) => {
return util.spinnify('Creating Apps Script project...', async () => {
await appsscript.create(projectPath, projectName);
// Since clasp creating a new project overwrites the manifest, we want to
// copy the template manifest over the one generated by clasp.
files.mv([execOptions.cwd, 'temp', 'appsscript.json'], [execOptions.cwd, 'src']);
if (config.authType === types_1.AuthType.OAUTH2) {
const fileOptions = { encoding: 'utf8' };
const manifestPath = path.resolve(projectPath, 'src', 'appsscript.json');
const manifestString = await fs.readFile(manifestPath, fileOptions);
const manifest = JSON.parse(manifestString);
// Add the OAUTH2_LIBRARY dependency.
manifest.dependencies.libraries.push(OAUTH2_LIBRARY);
await fs.writeFile(manifestPath, JSON.stringify(manifest, undefined, ' '), fileOptions);
}
await appsscript.push(projectPath);
});
};
const cloneAppsScriptProject = async (projectPath, config) => {
const scriptId = config.scriptId;
if (config.ts === true) {
// The user is trying to migrate an existing project to be an appsscript
// one.
return util.spinnify('Cloning existing project...', async () => {
await appsscript.clone(projectPath, scriptId, 'old_js');
files.cp([projectPath, 'old_js', 'appsscript.json'], [projectPath, 'src', 'appsscript.json']);
});
}
else {
// We don't need the template source files since we want the Apps Scripts project's
return util.spinnify('Cloning existing project...', async () => {
files.remove(projectPath, 'src');
await appsscript.clone(projectPath, scriptId, 'src');
});
}
};
const manageDeployments = async (projectPath, config) => {
let productionDeploymentId;
if (config.scriptId !== undefined) {
// See if there is already a 'Production' deployment.
productionDeploymentId = await util.spinnify('Checking for a production deployment', async () => {
return appsscript.getDeploymentIdByName(projectPath, 'Production');
});
}
if (productionDeploymentId === undefined) {
productionDeploymentId = await util.spinnify('Creating a production deployment', async () => {
return await appsscript.deploy(projectPath, 'Production');
});
}
const latestDeploymentId = await util.spinnify('Getting the latest deploymentId', async () => {
return await appsscript.getDeploymentIdByName(projectPath, '@HEAD');
});
if (latestDeploymentId === undefined) {
throw new Error(`Wasn't able to get the latest deploymentId. This is probably a bug with dscc-gen.`);
}
return util.spinnify('Updating templates with your values...', async () => {
return files.fixTemplates(projectPath, [
{ match: /{{PRODUCTION_DEPLOYMENT_ID}}/, replace: productionDeploymentId },
{ match: /{{LATEST_DEPLOYMENT_ID}}/, replace: latestDeploymentId },
]);
});
};
const authTypeToFile = {
[types_1.AuthType.NONE]: 'NONE_auth',
[types_1.AuthType.USER_PASS]: 'USER_PASS_auth',
[types_1.AuthType.USER_TOKEN]: 'USER_TOKEN_auth',
[types_1.AuthType.OAUTH2]: 'OAUTH2_auth',
[types_1.AuthType.KEY]: 'KEY_auth',
[types_1.AuthType.PATH_USER_PASS]: 'PATH_USER_PASS_auth',
};
const removeExcessAuthFiles = async (projectPath, config) => {
const extension = config.ts ? '.ts' : '.js';
const chosenAuthType = config.authType;
return Promise.all(Object.values(types_1.AuthType).map(async (authType) => {
const authFile = authTypeToFile[authType] + extension;
if (authType !== chosenAuthType) {
return files.remove(projectPath, 'src', authFile);
}
else {
const projectAuthFile = 'auth' + extension;
return files.rename([projectPath, 'src', authFile], [projectPath, 'src', projectAuthFile]);
}
}));
};
exports.createFromTemplate = async (config) => {
const { projectName, basePath } = config;
const templatePath = path.join(basePath, 'templates', config.projectChoice.toString() + (config.ts ? '-ts' : ''));
const projectPath = path.join(constants_1.PWD, projectName);
await files.createAndCopyFiles(projectPath, templatePath, projectName);
try {
await util.spinnify('Updating templates with your values...', async () => {
await files.fixTemplates(projectPath, getTemplates(config));
await removeExcessAuthFiles(projectPath, config);
});
const execOptions = { cwd: projectPath };
await installDependencies(projectPath, config);
await ensureAuthenticated(execOptions);
if (config.scriptId !== undefined) {
await cloneAppsScriptProject(projectPath, config);
}
else {
await createAppsScriptProject(projectPath, projectName, execOptions, config);
}
await manageDeployments(projectPath, config);
// Remove temp directory.
files.remove(projectPath, 'temp');
const connectorOverview = util_1.format.blue(terminal_link_1.default('connector overview', 'https://developers.google.com/datastudio/connector/'));
const styledProjectName = util_1.format.green(projectName);
const cdDirection = util_1.format.yellow(`cd ${projectName}`);
const runCmd = config.yarn ? 'yarn' : 'npm run';
const open = util_1.format.red(`${runCmd} open`);
const push = util_1.format.blue(`${runCmd} push`);
const watch = util_1.format.green(`${runCmd} watch`);
const prettier = util_1.format.yellow(`${runCmd} prettier`);
const tryLatest = util_1.format.red(`${runCmd} try_latest`);
const tryProduction = util_1.format.blue(`${runCmd} try_production`);
const updateProduction = util_1.format.green(`${runCmd} update_production`);
console.log(`\
Created a new community connector: ${styledProjectName}\n\
\n\
If this is your first connector, see ${connectorOverview}\n\
\n\
${cdDirection} to start working on your connector\n\
\n\
Scripts are provided to simplify development:\n\
\n\
${open} - open your project in Apps Script.\n\
${push} - push your local changes to Apps Script.\n\
${watch} - watches for local changes & pushes them to Apps Script.\n\
${prettier} - formats your code using community standards.\n\
${tryLatest} - opens the deployment with your latest code.\n\
${tryProduction} - opens your production deployment.\n\
${updateProduction} - updates your production deployment to use the latest code.\n\
`);
return 0;
}
catch (e) {
files.remove(projectPath);
throw e;
}
};
//# sourceMappingURL=index.js.map