import * as vscode from 'vscode'; import * as extract from 'extract-zip'; import * as fs from 'fs'; export async function generateSample() { let folderUris = await vscode.window.showOpenDialog({ canSelectFolders: true, canSelectFiles: false, canSelectMany: false, openLabel: 'Select folder', }); if (!folderUris) { return; } let framework = await pickFramework(); let projectName = ((await getProjectName()) ?? '').replace(/ /g, '-'); if (!fs.existsSync(folderUris[0].fsPath + '/' + projectName)) { const fromPath = vscode.extensions.getExtension('PayPay.paypay-devtool')?.extensionPath + '/resources/sample-' + framework + '.zip'; const toPath = folderUris[0].fsPath; try { await extract(fromPath, { dir: toPath }); if (projectName) { const projectPath = toPath + '/' + projectName; fs.renameSync(toPath + '/sample-' + framework, projectPath); let appfilePath: string[] = []; findFilePath(projectPath, 'app.json', appfilePath); updateAppName(appfilePath, projectName); updatePackageName(projectPath, projectName); } } catch (err) { vscode.window.showErrorMessage('Error generating sample files.'); } await vscode.commands.executeCommand( 'vscode.openFolder', vscode.Uri.parse(folderUris[0] + '/' + projectName) ); } else { vscode.window.showInformationMessage( `Project name already exists in the workspace` ); } } function findFilePath( path: string, serchedFile: string, filePath: Array ) { fs.readdirSync(path).forEach(function (file) { var subpath = path + '/' + file; if (fs.lstatSync(subpath).isDirectory()) { findFilePath(subpath, serchedFile, filePath); } else { if (file === serchedFile) { filePath.push(path + '/' + file); } } }); } function updateAppName(filePath: Array, projectName: string) { if (filePath.length) { var appJson = fs.readFileSync(filePath[0], 'utf-8'); if (appJson) { let newJson = JSON.parse(appJson); newJson.appName = projectName; fs.writeFileSync(filePath[0], JSON.stringify(newJson, null, 2), 'utf-8'); } } } function updatePackageName(filePath: string, projectName: string) { if (filePath.length) { const fileName = filePath + '/package.json'; var packageJson = fs.readFileSync(fileName, 'utf-8'); if (packageJson) { let newJson = JSON.parse(packageJson); newJson.name = projectName; fs.writeFileSync(fileName, JSON.stringify(newJson, null, 2), 'utf-8'); } } } async function getProjectName() { return await vscode.window.showInputBox({ value: '', placeHolder: 'Please enter a Project Name', }); } async function pickFramework() { return await vscode.window.showQuickPick( [ 'Javascript', 'React', 'React_Typescript', 'Typescript', 'Vuejs', 'Vuejs_Typescript', ], { placeHolder: 'Please choose a Framework', } ); }