const fs = require('fs'); const path = require('path'); var spawn = require('child_process').spawn; const srcPath = './node_modules/@gluestack/glue-plugin-storybook-aliasing/src/helpers'; const addDependencies = async (webFolder: string) => { const packageJsonPath = `${webFolder}/package.json`; // Read in the existing package.json file const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); // Add a new dependency to the package.json file packageJson.dependencies = packageJson.dependencies || {}; packageJson.dependencies['@dank-style/react'] = '0.1.12'; // Add a new devDependency to the package.json file packageJson.devDependencies = packageJson.devDependencies || {}; packageJson.devDependencies['react-native-web'] = '^0.18.10'; packageJson.devDependencies['next-compose-plugins'] = '^2.2.1'; packageJson.devDependencies['next-transpile-modules'] = '^10.0.0'; // Write the updated package.json file fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); console.log(`Added new dependency "new-dependency" to ${packageJsonPath}`); }; const updateTsConfig = async (webFolder: string, storybookFolder: string) => { const tsConfigJsonPath = `${webFolder}/tsconfig.json`; // Read in the existing package.json file const tsConfigJson = JSON.parse(fs.readFileSync(tsConfigJsonPath, 'utf8')); // Add a new dependency to the package.json file tsConfigJson.compilerOptions = tsConfigJson.compilerOptions || {}; tsConfigJson.compilerOptions['baseUrl'] = '.'; tsConfigJson.compilerOptions['paths'] = tsConfigJson.compilerOptions['paths'] || {}; tsConfigJson.compilerOptions['paths']['@gluestack/storybook'] = [ `${storybookFolder}/src/index`, ]; // Write the updated package.json file fs.writeFileSync(tsConfigJsonPath, JSON.stringify(tsConfigJson, null, 2)); console.log(`Updated tsconfig.json successfully!`); }; const replaceNextConfig = async ( webFolder: string, storybookFolder: string ) => { const nextConfigPath = `${webFolder}/next.config.js`; const newNextConfigPath = path.resolve(`${srcPath}/nextConfigFile.js`); const newNextConfig = fs.readFileSync(newNextConfigPath, 'utf8'); const storybookFolderUpdated = `../${storybookFolder.split('/').pop()}`; let modifiedNextConfig = newNextConfig.replace( /---STORYBOOK-INSTANCE-PATH---/gm, storybookFolderUpdated ); fs.writeFileSync(nextConfigPath, modifiedNextConfig); console.log(`Next config File is updated successfully.`); }; const addDocumentJs = async (webFolder: string) => { const documentPath = path.resolve(`${webFolder}/pages/_document.tsx`); const newDocumentPath = path.resolve(`${srcPath}/documentFile.js`); const newDocument = fs.readFileSync(newDocumentPath, 'utf8'); fs.writeFile(documentPath, newDocument, function (err: any) { if (err) throw err; console.log('Document File is created successfully.'); }); }; const addUIConfigJs = async (webFolder: string) => { const uiConfigPath = `${webFolder}/ui.config.ts`; const newUIConfigPath = path.resolve(`${srcPath}/uiConfigFile.js`); const newUIConfig = fs.readFileSync(newUIConfigPath, 'utf8'); fs.writeFile(uiConfigPath, newUIConfig, function (err: any) { if (err) throw err; console.log('UI Config File is created successfully.'); }); }; const changeDirectory = async (webFolder: string) => { process.chdir(webFolder); }; const yarnInstall = async () => { // exec('yarn', (error: any, stdout: any) => { // if (error) { // console.error(`exec error: ${error}`); // return; // } // console.log(stdout); // }); var ls = spawn('yarn'); ls.stdout.on('data', function (data: any) { console.log('stdout: ' + data.toString()); }); ls.stderr.on('data', function (data: any) { console.log('stderr: ' + data.toString()); }); ls.on('exit', function (code: any) { console.log('child process exited with code ' + code.toString()); }); }; const deleteFolderRecursive = async (path: string) => { if (fs.existsSync(path)) { fs.readdirSync(path).forEach(function (file: any, index: any) { var curPath = path + '/' + file; if (fs.lstatSync(curPath).isDirectory()) { // recurse deleteFolderRecursive(curPath); } else { // delete file fs.unlinkSync(curPath); } }); fs.rmdirSync(path); } console.log('Cleaned up auto-aliasing folder!'); }; export { addDependencies, updateTsConfig, replaceNextConfig, addDocumentJs, addUIConfigJs, changeDirectory, yarnInstall, deleteFolderRecursive, };