import * as path from 'path'; import * as fs from 'fs'; import * as childProcess from 'child_process'; import * as glob from 'glob'; import * as Routes from './Routes'; import * as Compiler from '../compiler/Compiler'; import * as FileUtils from '../util/FileUtils'; import * as Arr from '../util/Arr'; import * as Types from '../compiler/Types'; interface PackageJson { readonly name: string; readonly workspaces: string[]; } interface WorkspaceRoot { name: string; folder: string; } interface GenerateArgs { readonly mode: string; readonly projectdir: string; readonly basedir: string; readonly scratchdir: string; readonly configFile: string; readonly bundler: Types.Bundler; readonly testfiles: string[]; readonly chunk: number; readonly retries: number; readonly singleTimeout: number; readonly stopOnFailure: boolean; readonly basePage: string; readonly coverage: string[]; readonly polyfills: string[]; readonly skipTypecheck: boolean; } export const generate = async (args: GenerateArgs): Promise => { const { mode, projectdir, basedir, scratchdir, configFile, bundler, testfiles, chunk, retries, singleTimeout, stopOnFailure, basePage, coverage, polyfills, skipTypecheck } = args; const files = testfiles.map((filePath) => { return path.relative(projectdir, filePath); }); const testGenerator = Compiler.compile({ bundler, tsConfigFile: path.join(projectdir, configFile), scratchDir: path.join(projectdir, scratchdir), basedir, exitOnCompileError: mode === 'auto', srcFiles: files, coverage, polyfills, skipTypecheck }); // read the project json file to determine the project name to expose resources as `/project/${name}` const pkjson: PackageJson = FileUtils.readFileAsJson(`${projectdir}/package.json`); // Search for yarn workspace projects to use as resource folders const findWorkspaceResources = (moduleFolder: string): Array => { const moduleJson = `${moduleFolder}/package.json`; if (fs.statSync(moduleJson)) { const workspaceJson = FileUtils.readFileAsJson(moduleJson); return [{name: workspaceJson.name, folder: moduleFolder}]; } else { return []; } }; const findPnpmWorkspaces = async (): Promise => { if (!fs.existsSync(path.join(projectdir, 'pnpm-workspace.yaml'))) { return []; } return new Promise((resolve, reject) => childProcess.exec('pnpm list -r --only-projects --json', (err, stdout, stderr) => { if (stderr) console.error(stderr); if (err) { reject(err); return; } const result: WorkspaceRoot[] = []; for (const p of JSON.parse(stdout) as { name: string; path: string }[]) { const folder = path.relative(projectdir, p.path); if (!folder.length) continue; result.push({ name: p.name, folder }); } resolve(result); }) ); }; const workspaceRoots: WorkspaceRoot[] = ( pkjson.workspaces ? Arr.bind2(pkjson.workspaces, (w) => glob.sync(w), findWorkspaceResources) : await findPnpmWorkspaces() ); const resourceRoots = [{name: pkjson.name, folder: '.'}].concat(workspaceRoots); // console.log(`Resource maps from ${projectdir}: \n`, resourceRoots.map(({ name, folder }) => `/project/${name}/ => ${folder}`)); const nodeModuleRoutes = resourceRoots.map(({name, folder}) => Routes.nodeResolve('GET', `/project/${name}/node_modules`, path.join(projectdir, folder))); const resourceRoutes = resourceRoots.map(({name, folder}) => Routes.routing('GET', `/project/${name}`, path.join(projectdir, folder))); const compiledTests = mode === 'auto' ? Promise.resolve(await testGenerator.generate()) : testGenerator.generate(); const routers = [ ...nodeModuleRoutes, ...resourceRoutes, ...[ Routes.nodeResolve('GET', '/project/node_modules', projectdir), // fallback resource route to project root Routes.routing('GET', '/project', projectdir), // bedrock resources Routes.routing('GET', '/runner', path.join(require.resolve('@ephox/bedrock-runner'), '../../../../../dist')), Routes.routing('GET', '/lib/jquery', path.dirname(require.resolve('jquery'))), Routes.routing('GET', '/lib/core-js-bundle', path.dirname(require.resolve('core-js-bundle'))), Routes.routing('GET', '/css', path.join(basedir, 'src/resources/css')), Routes.nodeResolveFile('GET', '/agar-sw.js', projectdir, '@ephox/agar-sw', 'dist/agar-sw.js'), // test code Routes.asyncJs('GET', '/compiled/tests.js', compiledTests), Routes.routing('GET', '/compiled', path.join(projectdir, scratchdir, 'compiled')), // harness API Routes.json('GET', '/harness', { stopOnFailure, chunk, retries, timeout: singleTimeout, mode }) ] ]; const fallback = Routes.constant('GET', basedir, basePage); return { routers, fallback }; };