import { expect } from 'chai'; import path from 'node:path'; import { promises as fsp } from 'node:fs'; import { writeCollectionModuleStaticFiles } from '../static-file-helpers'; import { existsAsync, rmAsync } from '../../fs-helpers'; describe('writeCollectionModuleStaticFiles', function () { const directory = path.join('.', 'test-cm-static-files'); before('Create test directory', async function () { if (await existsAsync(directory)) { await rmAsync(directory, { recursive: true, force: true }); } await fsp.mkdir(directory); }); after('Delete test directory', async function () { await rmAsync(directory, { recursive: true, force: true }); }); it('should merge package.json correctly', async function () { const existingPackageJson = { name: 'test', version: '1.0.0', devDependencies: { '@types/node': '^16.6.1', }, }; await writeCollectionModuleStaticFiles({ directory, existingStaticFiles: { packageJson: existingPackageJson, gitIgnore: [], }, }); const packageJson = JSON.parse(await fsp.readFile(path.join(directory, 'package.json'), 'utf8')); expect(packageJson).to.deep.equal({ name: 'test', version: '1.0.0', devDependencies: { '@types/node': '^16.6.1', }, dependencies: { joi: '11.4.0', 'moment-timezone': '0.5.40', 'node-fetch': '2.6.7', phone: '2.4.22', stripe: '14.5.0', '@rootplatform/node-sdk': '^0.0.7', }, }); }); it('should merge .gitignore correctly', async function () { const existingGitIgnore = ['.root-auth', '#This is a comment', 'code/**/env.*', '']; await writeCollectionModuleStaticFiles({ directory, existingStaticFiles: { gitIgnore: existingGitIgnore, }, }); const gitIgnore = (await fsp.readFile(path.join(directory, '.gitignore'), 'utf8')).split('\n').map((l) => l.trim()); expect(gitIgnore).to.deep.equal(['.root-auth', '#This is a comment', 'code/**/env.*', '', 'node_modules', '']); }); it('should merge tsconfig.json correctly', async function () { const existingTsConfig = { parent: 'test', compilerOptions: { child: 'test', }, }; await writeCollectionModuleStaticFiles({ directory, existingStaticFiles: { tsConfig: existingTsConfig, gitIgnore: [], }, }); const tsConfig = JSON.parse(await fsp.readFile(path.join(directory, 'tsconfig.json'), 'utf8')); expect(tsConfig).to.deep.equal({ include: ['./code/**/*'], parent: 'test', compilerOptions: { child: 'test', lib: ['es2022'], module: 'es2022', skipLibCheck: true, allowJs: true, strict: true, moduleResolution: 'Bundler', noEmit: true, }, }); }); });