'use strict' ; import { join , resolve , dirname } from 'path' ; import { execSync } from 'child_process' ; import { CheckupConfig , mergeConfig , FilePathArray } from '@checkup/core' ; import * as fixturify from 'fixturify' ; import Project from 'fixturify-project' ; import stringify from 'json-stable-stringify' ; import type { PackageJson } from 'type-fest' ; import { symlinkSync , mkdirpSync } from 'fs-extra' ; const walkSync = require ( 'walk-sync' ) ; const ROOT = process . cwd ( ) ; class augments Project export default class CheckupFixturifyProject extends Project { private _hasWritten : boolean = false ; private _dirChanged : boolean = false ; constructor ( name , version ? , cb ? ) { super ( name , version , cb ) ; this . pkg = Object . assign ( { } , this . pkg , { license : 'MIT' , description : 'Fake project' , repository : 'http://fakerepo.com' , } ) ; } write ( dirJSON ) { Object . assign ( this . files , dirJSON ) ; this . writeSync ( ) ; } writeSync ( ) { super . writeSync ( ) ; this . _hasWritten = true ; } symlinkPackage ( source , target ) { mkdirpSync ( dirname ( target ) ) ; symlinkSync ( source , target ) ; } memberof param { CheckupConfig } config addCheckupConfig ( config = { } ) { this . files [ '.checkuprc' ] = stringify ( mergeConfig ( config ) ) ; this . files [ '.eslintrc.js' ] = '' ; this . writeSync ( ) ; } memberof gitInit ( ) { try { execSync ( `git init -q ${ this . baseDir }` ) ; } catch { throw new Error ( "Couldn't initialize git repository." ) ; } } install ( cwd = this . baseDir ) { let cmd = 'yarn install --silent' ; try { execSync ( cmd , { cwd , stdio : 'ignore' } ) ; } catch ( error ) { throw new Error ( error ) ; } } chdir ( ) { this . _dirChanged = true ; this . writeSync ( ) ; process . chdir ( this . baseDir ) ; } dispose ( ) { if ( this . _dirChanged ) { process . chdir ( ROOT ) ; } return super . dispose ( ) ; } param { PackageJson } packageJson memberof updatePackageJson ( packageJson ) { packageJson . name = this . name ; this . pkg = packageJson ; } get filePaths ( ) : FilePathArray { if ( this . _hasWritten ) { let allFiles = walkSync ( this . baseDir , { directories : false , ignore : [ 'node_modules' ] } ) ; return new FilePathArray ( ... resolveFilePaths ( this . baseDir , allFiles ) ) ; } else { throw new Error ( 'You must call writeSync on your project before getting the file paths.' ) ; } } } param { string } baseDir param { string [ ] } filePaths returns { * } function resolveFilePaths ( baseDir , filePaths ) { if ( baseDir !== '.' ) { return filePaths . map ( ( pathName ) => { return join ( resolve ( baseDir ) , pathName ) ; } ) ; } return filePaths ; }