Skip to content

Commit

Permalink
fix(pencil): Copy on failed symlink for Windows
Browse files Browse the repository at this point in the history
Closes #89, #23, #103, #24.
  • Loading branch information
JackCuthbert committed Apr 6, 2019
1 parent 41e4034 commit f319394
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/index.ts
Expand Up @@ -136,14 +136,17 @@ export class TypeScriptPlugin {
}

async copyExtras() {
// include node_modules into build
if (!fs.existsSync(path.resolve(path.join(buildFolder, 'node_modules')))) {
fs.symlinkSync(path.resolve('node_modules'), path.resolve(path.join(buildFolder, 'node_modules')), 'junction')
const outPkgPath = path.resolve(path.join(buildFolder, 'package.json'))
const outModulesPath = path.resolve(path.join(buildFolder, 'node_modules'))

// Link or copy node_modules and package.json to .build so Serverless can
// exlcude devDeps during packaging
if (!fs.existsSync(outModulesPath)) {
await this.linkOrCopy(path.resolve('node_modules'), outModulesPath, 'junction')
}

// include package.json into build so Serverless can exlcude devDeps during packaging
if (!fs.existsSync(path.resolve(path.join(buildFolder, 'package.json')))) {
fs.symlinkSync(path.resolve('package.json'), path.resolve(path.join(buildFolder, 'package.json')), 'file')
if (!fs.existsSync(outPkgPath)) {
await this.linkOrCopy(path.resolve('package.json'), outPkgPath, 'file')
}

// include any "extras" from the "include" section
Expand Down Expand Up @@ -209,6 +212,23 @@ export class TypeScriptPlugin {
fs.removeSync(path.join(this.originalServicePath, buildFolder))
}

/**
* Attempt to symlink a given path or directory and copy if it fails with an
* `EPERM` error.
*/
private async linkOrCopy(
srcPath: string,
dstPath: string,
type?: 'dir' | 'junction' | 'file'
): Promise<void> {
return fs.symlink(srcPath, dstPath, type)
.catch(error => {
if (error.code === 'EPERM' && error.errno === -4048) {
return fs.copy(srcPath, dstPath)
}
throw error
})
}
}

module.exports = TypeScriptPlugin

0 comments on commit f319394

Please sign in to comment.