From 067208df0a3d1ef85553c7e9b30b534af205c1cb Mon Sep 17 00:00:00 2001 From: Chad Ostrowski <221614+chadoh@users.noreply.github.com> Date: Thu, 26 Nov 2020 14:23:26 -0500 Subject: [PATCH] build: ignore contract build artifacts In the course of local development, contract directories such as common/contract/assemblyscript may end up with lockfiles and build files like yarn.lock and node_modules. However, these files are gitignored and will not be published with the package on NPM. This can lead to discrepencies between the versions installed when using the create-near-app project from a cloned repository vs using the package from NPM. This refactors index.js file to make it easier to skip such files, so that creating a project from a cloned version of create-near-app gives similar results to using the package from NPM. --- index.js | 73 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/index.js b/index.js index fef2780b4..4e774871d 100755 --- a/index.js +++ b/index.js @@ -24,52 +24,63 @@ const renameFile = async function(oldPath, newPath) { }) } +// Wrap `ncp` tool to wait for the copy to finish when using `await` +// Allow passing `skip` variable to skip copying an array of filenames +function copyDir (source, dest, { skip, veryVerbose } = {}) { + return new Promise((resolve, reject) => { + const copied = [] + const skipped = [] + const filter = skip && function (filename) { + const shouldCopy = !skip.find(f => filename.includes(f)) + shouldCopy ? copied.push(filename) : skipped.push(filename) + return !skip.find(f => filename.includes(f)) + } + + ncp(source, dest, { filter }, (err) => { + if (err) return reject(err) + + if (veryVerbose) { + console.log('Copied:') + copied.forEach(f => console.log(' ' + f)) + console.log('Skipped:') + skipped.forEach(f => console.log(' ' + f)) + } + + resolve() + }) + }) +} + const createProject = async function({ contract, frontend, projectDir, veryVerbose }) { const templateDir = `/templates/${frontend}` const sourceTemplateDir = __dirname + templateDir console.log(`Copying files to new project directory (${projectDir}) from template source (${sourceTemplateDir}).`) - // Need to wait for the copy to finish, otherwise next tasks do not find files. - const copyDirFn = (source, dest, opts = {}) => { - return new Promise((resolve, reject) => { - ncp(source, dest, opts, (err) => { - if (err) return reject(err) - resolve() - }) - }) - } - // our frontend templates are set up with symlinks for easy development, - // developing right in these directories also results in build artifacts; - // we don't want to copy these - const filesToSkip = [ + await copyDir(sourceTemplateDir, projectDir, { veryVerbose, skip: [ + // our frontend templates are set up with symlinks for easy development, + // developing right in these directories also results in build artifacts; + // we don't want to copy these + path.join(sourceTemplateDir, '.cache'), + path.join(sourceTemplateDir, 'dist'), + path.join(sourceTemplateDir, 'out'), path.join(sourceTemplateDir, 'node_modules'), path.join(sourceTemplateDir, 'yarn.lock'), path.join(sourceTemplateDir, 'package-lock.json'), path.join(sourceTemplateDir, 'contract'), ...sh.ls(`${__dirname}/common/frontend`).map(f => path.join('src', f)) - ] - const copied = [] - const skipped = [] - await copyDirFn(sourceTemplateDir, projectDir, { - filter: filename => { - const shouldCopy = !filesToSkip.find(f => filename.includes(f)) - shouldCopy ? copied.push(filename) : skipped.push(filename) - return !filesToSkip.find(f => filename.includes(f)) - } - }) + ]}) - if (veryVerbose) { - console.log('Copied:') - copied.forEach(f => console.log(' ' + f)) - console.log('Skipped:') - skipped.forEach(f => console.log(' ' + f)) - } // copy common files + await copyDir(`${__dirname}/common/frontend`, `${projectDir}/src`) const contractSourceDir = `${__dirname}/common/contracts/${contract}` - await copyDirFn(contractSourceDir, `${projectDir}/contract`) - await copyDirFn(`${__dirname}/common/frontend`, `${projectDir}/src`) + await copyDir(contractSourceDir, `${projectDir}/contract`, { veryVerbose, skip: [ + // as above, skip rapid-development build artifacts + path.join(contractSourceDir, 'node_modules'), + path.join(contractSourceDir, 'yarn.lock'), + path.join(contractSourceDir, 'package-lock.json'), + ]}) // update package name let projectName = basename(resolve(projectDir))