Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path, logging, cleanup fixes #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
const symlink = require('./src/symlink');

class PackageCommon {
constructor(serverless, options) {
constructor(serverless, options, { log }) {
this.log = log;
this.serverless = serverless;
this.options = Object.assign({
common: []
Expand All @@ -12,37 +13,36 @@ class PackageCommon {
this.symlinked = false;

this.hooks = {
'before:package:createDeploymentArtifacts': this.beforeDeploy.bind(this),
'after:deploy:deploy': this.afterDeploy.bind(this)
'before:package:createDeploymentArtifacts': this.beforePackage.bind(this),
'after:package:finalize': this.afterPackage.bind(this)
};

this.handleExit();
}

beforeDeploy() {
beforePackage() {
// Symlink common folders
return Promise.all(this.options.common.map(commonFolder => {
this.symlinked = true;
return symlink.createFolder(commonFolder, this.serverless);
}))
.then(() => {
this.serverless.cli.log(`[serverless-package-common] Package Common is complete`);
this.log.success('Common packaging complete');
});
}

afterDeploy() {
afterPackage() {
if(this.symlinked) {
this.options.common.forEach(commonFolder => {
const target = commonFolder.replace(/..\//g, '');
symlink.removeFolder(target);
symlink.removeFolder(commonFolder);
});
}
}

handleExit(func) {
['SIGINT', 'SIGTERM', 'SIGQUIT']
.forEach(signal => process.on(signal, () => {
this.afterDeploy();
this.afterPackage();
}));
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/symlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs');
const path = require('path');
const rimraf = require('rimraf');
const yesno = require('yesno');
const { log } = require('@serverless/utils/log');

const exists = path => {
try {
Expand Down Expand Up @@ -33,22 +34,30 @@ const askToOverwrite = (targetExists, folder) => {
return ask;
};

// Build symlink path
const symlinkTarget = folder => {
return path.join(process.cwd(), path.basename(folder));
};

// Symlink a folder
const createFolder = (folder, serverless) => {
const target = path.join(process.cwd(), folder.replace(/..\//g, ''));
const createFolder = folder => {
const target = symlinkTarget(folder);

// Check if folder/file with symlink name already exists in top level
return askToOverwrite(exists(target) ? [target] : [], folder)
.then(() => {
// There is either no conflict or the user has accepted overwriting
serverless.cli.log(`[serverless-package-common] Symlinking ${folder}`);
log.verbose(`Symlinking ${folder} to target ${target}`);
rimraf.sync(target);
fs.symlinkSync(folder, target);
});
};

const removeFolder = folder => {
rimraf.sync(path.join(process.cwd(), folder));
const target = symlinkTarget(folder);
log.verbose(`Removing symlink ${target}`);

rimraf.sync(target);
};

module.exports = { createFolder, removeFolder };