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

Support prerendered apps #147

Open
wants to merge 2 commits 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
12 changes: 12 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = {
semi: true,
singleQuote: true,
useTabs: false,
quoteProps: 'consistent',
bracketSpacing: true,
arrowParens: 'always',
printWidth: 120,
trailingComma: 'none'
};
156 changes: 101 additions & 55 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/* jshint node: true */
'use strict';

let path = require('path');
let fs = require('fs');
const { glob } = require('glob');
const path = require('path');
const fs = require('fs');

let DeployPluginBase = require('ember-cli-deploy-plugin');
const DeployPluginBase = require('ember-cli-deploy-plugin');

module.exports = {
name: 'ember-cli-deploy-redis',

createDeployPlugin(options) {
var Redis = require('./lib/redis');
const Redis = require('./lib/redis');

var DeployPlugin = DeployPluginBase.extend({
const DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
host: 'localhost',
Expand All @@ -28,26 +29,29 @@ module.exports = {
distDir(context) {
return context.distDir;
},
keyPrefix(context){
keyPrefix(context) {
return `${context.project.name()}:index`;
},
activationSuffix: 'current',
activeContentSuffix: 'current-content',
didDeployMessage(context){
var revisionKey = context.revisionData && context.revisionData.revisionKey;
var activatedRevisionKey = context.revisionData && context.revisionData.activatedRevisionKey;
didDeployMessage(context) {
const revisionKey = context.revisionData && context.revisionData.revisionKey;
const activatedRevisionKey = context.revisionData && context.revisionData.activatedRevisionKey;
if (revisionKey && !activatedRevisionKey) {
return `Deployed but did not activate revision ${revisionKey}. `
+ `To activate, run: `
+ `ember deploy:activate ${context.deployTarget} --revision=${revisionKey}` + "\n";
return (
`Deployed but did not activate revision ${revisionKey}. ` +
`To activate, run: ` +
`ember deploy:activate ${context.deployTarget} --revision=${revisionKey}` +
'\n'
);
}
},
revisionKey(context) {
return context.commandOptions.revision || (context.revisionData && context.revisionData.revisionKey);
},
redisDeployClient(context, pluginHelper) {
let redisLib = context._redisLib;
let libOptions = {
const redisLib = context._redisLib;
const libOptions = {
url: pluginHelper.readConfig('url'),
host: pluginHelper.readConfig('host'),
port: pluginHelper.readConfig('port'),
Expand All @@ -72,43 +76,81 @@ module.exports = {
if (!this.pluginConfig.url) {
['host', 'port'].forEach(this.applyDefaultConfigProperty.bind(this));
} else {
var redisUrlRegexp = new RegExp('^rediss?://');
const redisUrlRegexp = new RegExp('^rediss?://');

if (!this.pluginConfig.url.match(redisUrlRegexp)) {
throw new Error(`Your Redis URL appears to be missing the "redis://" protocol. Update your URL to: redis://${this.pluginConfig.url}`);
throw new Error(
`Your Redis URL appears to be missing the "redis://" protocol. Update your URL to: redis://${this.pluginConfig.url}`
);
}
}

['filePattern', 'distDir', 'keyPrefix', 'activationSuffix', 'activeContentSuffix', 'revisionKey', 'didDeployMessage', 'redisDeployClient', 'maxRecentUploads', 'revisionData'].forEach(this.applyDefaultConfigProperty.bind(this));
[
'filePattern',
'distDir',
'keyPrefix',
'activationSuffix',
'activeContentSuffix',
'revisionKey',
'didDeployMessage',
'redisDeployClient',
'maxRecentUploads',
'revisionData'
].forEach(this.applyDefaultConfigProperty.bind(this));

this.log('config ok', { verbose: true });
},

async upload(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let revisionKey = this.readConfig('revisionKey');
let distDir = this.readConfig('distDir');
let filePattern = this.readConfig('filePattern');
let keyPrefix = this.readConfig('keyPrefix');
let filePath = path.join(distDir, filePattern);

this.log(`Uploading \`${filePath}\``, { verbose: true });
try {
let fileContents = await this._readFileContents(filePath);
let key = await redisDeployClient.upload(keyPrefix, revisionKey, this.readConfig('revisionData'), fileContents);
this._logUploadSuccessMessage(key);
return { redisKey: key };
} catch(e) {
this._logErrorMessage(e);
throw e;
const redisDeployClient = this.readConfig('redisDeployClient');
const revisionKey = this.readConfig('revisionKey');
const distDir = this.readConfig('distDir');
const filePattern = this.readConfig('filePattern');
const keyPrefix = this.readConfig('keyPrefix');
const filePathGlob = path.join(distDir, '**', filePattern);

const filePaths = await glob(filePathGlob);

if (filePaths.length === 0) {
throw new Error(`No index files found using pattern: ${filePathGlob}`);
}

if (filePaths.length) {
const keys = [];
for (const filePath of filePaths) {
this.log(`Uploading \`${filePath}\``, { verbose: true });
try {
let prefix = keyPrefix;
const fileContents = await this._readFileContents(filePath);
const relativePath = path.relative(distDir, filePath);
const directoryName = path.dirname(relativePath);

if (directoryName !== '' && directoryName !== '.') {
prefix = `${keyPrefix.replace(':index', `:${directoryName}:index`)}`;
}

const key = await redisDeployClient.upload(
prefix,
revisionKey,
this.readConfig('revisionData'),
fileContents
);
this._logUploadSuccessMessage(key);
keys.push({ path: directoryName, redisKey: key });
} catch (e) {
this._logErrorMessage(e);
throw e;
}
}
return keys;
}
},

async willActivate(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let keyPrefix = this.readConfig('keyPrefix');
const redisDeployClient = this.readConfig('redisDeployClient');
const keyPrefix = this.readConfig('keyPrefix');

let previousRevisionKey = await redisDeployClient.activeRevision(keyPrefix);
const previousRevisionKey = await redisDeployClient.activeRevision(keyPrefix);
return {
revisionData: {
previousRevisionKey
Expand All @@ -117,11 +159,11 @@ module.exports = {
},

async activate(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let revisionKey = this.readConfig('revisionKey');
let keyPrefix = this.readConfig('keyPrefix');
let activationSuffix = this.readConfig('activationSuffix');
let activeContentSuffix = this.readConfig('activeContentSuffix');
const redisDeployClient = this.readConfig('redisDeployClient');
const revisionKey = this.readConfig('revisionKey');
const keyPrefix = this.readConfig('keyPrefix');
const activationSuffix = this.readConfig('activationSuffix');
const activeContentSuffix = this.readConfig('activeContentSuffix');

this.log(`Activating revision \`${revisionKey}\``, { verbose: true });
try {
Expand All @@ -132,52 +174,56 @@ module.exports = {
activatedRevisionKey: revisionKey
}
};
} catch(e) {
} catch (e) {
this._logErrorMessage(e);
throw e;
}
},

didDeploy(/* context */){
var didDeployMessage = this.readConfig('didDeployMessage');
didDeploy(/* context */) {
const didDeployMessage = this.readConfig('didDeployMessage');
if (didDeployMessage) {
this.log(didDeployMessage);
}
},

async fetchInitialRevisions(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let keyPrefix = this.readConfig('keyPrefix');
const redisDeployClient = this.readConfig('redisDeployClient');
const keyPrefix = this.readConfig('keyPrefix');

this.log(`Fetching initial revisions for key: \`${keyPrefix}\``, { verbose: true });
this.log(`Fetching initial revisions for key: \`${keyPrefix}\``, {
verbose: true
});
try {
let initialRevisions = await redisDeployClient.fetchRevisions(keyPrefix);
const initialRevisions = await redisDeployClient.fetchRevisions(keyPrefix);
return {
initialRevisions
};
} catch(e) {
} catch (e) {
this._logErrorMessage(e);
throw e;
}
},

async fetchRevisions(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let keyPrefix = this.readConfig('keyPrefix');
this.log(`Fetching revisions for key: \`${keyPrefix}\``, { verbose: true });
const redisDeployClient = this.readConfig('redisDeployClient');
const keyPrefix = this.readConfig('keyPrefix');
this.log(`Fetching revisions for key: \`${keyPrefix}\``, {
verbose: true
});
try {
let revisions = await redisDeployClient.fetchRevisions(keyPrefix);
const revisions = await redisDeployClient.fetchRevisions(keyPrefix);
return {
revisions
};
} catch(e) {
} catch (e) {
this._logErrorMessage(e);
throw e;
}
},

async _readFileContents(path) {
let buffer = await fs.promises.readFile(path);
const buffer = await fs.promises.readFile(path);
return buffer.toString();
},

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"chalk": "^4.1.1",
"core-object": "^3.1.5",
"glob": "^10.2.6",
"ember-cli-deploy-plugin": "^0.2.9",
"ioredis": "^4.28.5",
"rsvp": "^4.8.5"
Expand All @@ -28,7 +29,6 @@
"chai-as-promised": "^7.1.1",
"ember-cli": "^3.28.6",
"eslint": "^8.42.0",
"glob": "^10.2.6",
"ioredis-mock": "^5.9.1",
"lerna-changelog": "^1.0.1",
"mocha": "^8.4.0",
Expand Down
Loading