const vscode = require("vscode"); const path = require("path"); const fsExtra = require("fs-extra"); const util = require("./Util"); const rootPath = vscode.workspace.rootPath; class FSProvider { static checkExistProject() { vscode.window.showInformationMessage('check project'); const packageJson = path.join(rootPath, 'package.json'); const env = path.join(rootPath, '.env'); const srcFolder = path.join(rootPath, 'src'); vscode.window.showInformationMessage('check project xong'); return fsExtra.existsSync(packageJson) || fsExtra.existsSync(env) || fsExtra.existsSync(srcFolder); } static checkExist(distPath : string) { const file = path.join(rootPath, distPath); return fsExtra.existsSync(file); } static copyFile(assetPathFrom: string, pathTo: any) { try { const fullPath = path.join(rootPath, pathTo); fsExtra.copy(__dirname + '/assets/' + assetPathFrom, fullPath); } catch (error) { console.log('Error write file:', error); } } static copyAndReplaceFile(assetPathFrom: string, pathTo: any, keywords: any[]) { const fullPath = path.join(rootPath, pathTo); const buffer = fsExtra.readFileSync(__dirname + '/assets/' + assetPathFrom); let content = buffer.toString(); content = util.replaceSymbolTemplate(content); keywords.forEach(pair => { content = content.replace(pair.regex, pair.value); }); fsExtra.ensureFileSync(fullPath); fsExtra.writeFileSync(fullPath, content); } static makeFolder(folderPath: any) { const fullPath = path.join(rootPath, folderPath); fsExtra.ensureDirSync(fullPath); } static getAllFileInFolder(folderPath: any) { const fullPath = path.join(rootPath, folderPath); const files = fsExtra.readdirSync(fullPath); return files.map((file: string) => file = file.replace('.ts', '')); } static getLinesDocumentInFile(folderPath: any) { const fullPath = path.join(rootPath, folderPath); const files = fsExtra.readFileSync(fullPath); return files.toString().split('\n') || []; } static getPathFromRoot(folderPath :any){ return folderPath.replace(rootPath, ''); } } export default FSProvider;