Skip to content

Commit

Permalink
General repo cleanup. (#231)
Browse files Browse the repository at this point in the history
General repo cleanup.
  • Loading branch information
rwjblue authored Oct 24, 2019
2 parents 96eabc8 + 2500c23 commit 99c6f4d
Show file tree
Hide file tree
Showing 28 changed files with 941 additions and 743 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
!.*
test/fixtures
test/helpers
test/helpers
35 changes: 14 additions & 21 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
module.exports = {
'env': {
'browser': true,
'node': true,
'es6': true,
},
'extends': ['eslint:recommended', 'plugin:node/recommended'],
'parserOptions': {
'ecmaVersion': 6
},
'rules': {
'no-console': ['error', { 'allow': ['warn', 'error']}],
'linebreak-style': [
'error',
'unix'
],
'semi': [
'error',
'always'
]
}
};
parserOptions: {
ecmaVersion: 2017,
},
extends: ['eslint:recommended', 'plugin:node/recommended', 'plugin:prettier/recommended'],
plugins: ['prettier', 'node'],
env: {
browser: true,
node: true,
es6: true,
},
rules: {
'no-console': ['error', { allow: ['warn', 'error'] }],
},
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.eslintcache
/node_modules
npm-debug.log
tmp*
Expand Down
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"printWidth": 100,
"trailingComma": "es5"
}
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "src/index.js",
"scripts": {
"changelog": "lerna-changelog",
"lint:js": "node_modules/.bin/eslint src test",
"lint:js": "eslint --cache .",
"test": "npm run-script lint:js && mocha",
"preversion": "npm test",
"postversion": "git push origin master --tags"
Expand Down Expand Up @@ -41,13 +41,16 @@
"chai": "^4.1.0",
"chai-as-promised": "^7.1.1",
"ember-source": "3.8.0",
"eslint": "^5.10.0",
"eslint": "^6.5.1",
"eslint-config-prettier": "^6.4.0",
"eslint-plugin-chai-expect": "^2.0.1",
"eslint-plugin-mocha": "^5.2.0",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-mocha": "^6.2.0",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-prettier": "^3.1.1",
"express": "^4.15.4",
"lerna-changelog": "^0.8.2",
"mocha": "^5.2.0",
"prettier": "^1.18.2",
"rimraf": "^2.6.2",
"temp": "^0.8.3"
}
Expand Down
101 changes: 64 additions & 37 deletions src/ember-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class EmberApp {
if (process.env.APP_CONFIG) {
let appConfig = JSON.parse(process.env.APP_CONFIG);
let appConfigKey = this.appName;
if (!appConfig.hasOwnProperty(appConfigKey)) {
if (!(appConfigKey in appConfig)) {
this.config[appConfigKey] = appConfig;
}
}
Expand All @@ -71,9 +71,10 @@ class EmberApp {
* @param {Object} [sandboxGlobals={}] any additional variables to expose in the sandbox or override existing in the sandbox
*/
buildSandbox(distPath, sandboxClass, sandboxGlobals) {
const { config, appName } = this;

let sandboxRequire = this.buildWhitelistedRequire(this.moduleWhitelist, distPath);
const config = this.config;
const appName = this.appName;

function fastbootConfig(key) {
if (!key) {
// default to app key
Expand All @@ -93,10 +94,11 @@ class EmberApp {
FastBoot: {
require: sandboxRequire,
config: fastbootConfig,

get distPath() {
return distPath;
}
}
},
},
};

globals = Object.assign(globals, sandboxGlobals);
Expand All @@ -123,13 +125,13 @@ class EmberApp {
let isLegacyWhitelist = this.schemaVersion < FastBootSchemaVersions.strictWhitelist;

whitelist.forEach(function(whitelistedModule) {
debug("module whitelisted; module=%s", whitelistedModule);
debug('module whitelisted; module=%s', whitelistedModule);

if (isLegacyWhitelist) {
let packageName = getPackageName(whitelistedModule);

if (packageName !== whitelistedModule && whitelist.indexOf(packageName) === -1) {
console.error("Package '" + packageName + "' is required to be in the whitelist.");
console.error("Package '" + packageName + "' is required to be in the whitelist.");
}
}
});
Expand Down Expand Up @@ -161,11 +163,19 @@ class EmberApp {
return require(moduleName);
}
} else {
throw new Error("Unable to require module '" + moduleName + "' because it was not in the whitelist.");
throw new Error(
"Unable to require module '" + moduleName + "' because it was not in the whitelist."
);
}
}

throw new Error("Unable to require module '" + moduleName + "' because its package '" + packageName + "' was not in the whitelist.");
throw new Error(
"Unable to require module '" +
moduleName +
"' because its package '" +
packageName +
"' was not in the whitelist."
);
};
}

Expand All @@ -174,29 +184,29 @@ class EmberApp {
*
* Loads the app and vendor files in the sandbox (Node vm).
*
*/
*/
loadAppFiles() {
let sandbox = this.sandbox;
let appFilePaths = this.appFilePaths;
let vendorFilePaths = this.vendorFilePaths;

sandbox.eval('sourceMapSupport.install(Error);');

debug("evaluating app and vendor files");
debug('evaluating app and vendor files');

vendorFilePaths.forEach(function(vendorFilePath) {
debug("evaluating vendor file %s", vendorFilePath);
debug('evaluating vendor file %s', vendorFilePath);
let vendorFile = fs.readFileSync(vendorFilePath, 'utf8');
sandbox.eval(vendorFile, vendorFilePath);
});
debug("vendor file evaluated");
debug('vendor file evaluated');

appFilePaths.forEach(function(appFilePath) {
debug("evaluating app file %s", appFilePath);
debug('evaluating app file %s', appFilePath);
let appFile = fs.readFileSync(appFilePath, 'utf8');
sandbox.eval(appFile, appFilePath);
});
debug("app files evaluated");
debug('app files evaluated');
}

/**
Expand All @@ -215,7 +225,9 @@ class EmberApp {

// If the application factory couldn't be found, throw an error
if (!AppFactory || typeof AppFactory['default'] !== 'function') {
throw new Error('Failed to load Ember app from app.js, make sure it was built for FastBoot with the `ember fastboot:build` command.');
throw new Error(
'Failed to load Ember app from app.js, make sure it was built for FastBoot with the `ember fastboot:build` command.'
);
}

// Otherwise, return a new `Ember.Application` instance
Expand Down Expand Up @@ -325,20 +337,19 @@ class EmberApp {
let disableShoebox = options.disableShoebox || false;
let destroyAppInstanceInMs = parseInt(options.destroyAppInstanceInMs, 10);

let shouldRender = (options.shouldRender !== undefined) ? options.shouldRender : true;
let shouldRender = options.shouldRender !== undefined ? options.shouldRender : true;
let bootOptions = buildBootOptions(shouldRender);
let fastbootInfo = new FastBootInfo(
req,
res,
{ hostWhitelist: this.hostWhitelist, metadata: options.metadata }
);
let fastbootInfo = new FastBootInfo(req, res, {
hostWhitelist: this.hostWhitelist,
metadata: options.metadata,
});

let doc = bootOptions.document;

let result = new Result({
doc: doc,
html: html,
fastbootInfo: fastbootInfo
fastbootInfo: fastbootInfo,
});

let destroyAppInstanceTimer;
Expand All @@ -347,7 +358,9 @@ class EmberApp {
// This is a failure mechanism so that node process doesn't get wedged if the `visit` never completes.
destroyAppInstanceTimer = setTimeout(function() {
if (result._destroyAppInstance()) {
result.error = new Error('App instance was forcefully destroyed in ' + destroyAppInstanceInMs + 'ms');
result.error = new Error(
'App instance was forcefully destroyed in ' + destroyAppInstanceInMs + 'ms'
);
}
}, destroyAppInstanceInMs);
}
Expand All @@ -359,7 +372,7 @@ class EmberApp {
createShoebox(doc, fastbootInfo);
}
})
.catch(error => result.error = error)
.catch(error => (result.error = error))
.then(() => result._finalize())
.finally(() => {
if (result._destroyAppInstance()) {
Expand All @@ -381,7 +394,9 @@ class EmberApp {
try {
file = fs.readFileSync(pkgPath);
} catch (e) {
throw new Error(`Couldn't find ${pkgPath}. You may need to update your version of ember-cli-fastboot.`);
throw new Error(
`Couldn't find ${pkgPath}. You may need to update your version of ember-cli-fastboot.`
);
}

let manifest;
Expand All @@ -393,15 +408,23 @@ class EmberApp {
manifest = pkg.fastboot.manifest;
schemaVersion = pkg.fastboot.schemaVersion;
} catch (e) {
throw new Error(`${pkgPath} was malformed or did not contain a manifest. Ensure that you have a compatible version of ember-cli-fastboot.`);
throw new Error(
`${pkgPath} was malformed or did not contain a manifest. Ensure that you have a compatible version of ember-cli-fastboot.`
);
}

const currentSchemaVersion = FastBootSchemaVersions.latest;
// set schema version to 1 if not defined
schemaVersion = schemaVersion || FastBootSchemaVersions.base;
debug('Current schemaVersion from `ember-cli-fastboot` is %s while latest schema version is %s', schemaVersion, currentSchemaVersion);
debug(
'Current schemaVersion from `ember-cli-fastboot` is %s while latest schema version is %s',
schemaVersion,
currentSchemaVersion
);
if (schemaVersion > currentSchemaVersion) {
let errorMsg = chalk.bold.red('An incompatible version between `ember-cli-fastboot` and `fastboot` was found. Please update the version of fastboot library that is compatible with ember-cli-fastboot.');
let errorMsg = chalk.bold.red(
'An incompatible version between `ember-cli-fastboot` and `fastboot` was found. Please update the version of fastboot library that is compatible with ember-cli-fastboot.'
);
throw new Error(errorMsg);
}

Expand All @@ -421,13 +444,13 @@ class EmberApp {
}
}

debug("reading array of app file paths from manifest");
var appFiles = manifest.appFiles.map(function(appFile) {
debug('reading array of app file paths from manifest');
let appFiles = manifest.appFiles.map(function(appFile) {
return path.join(distPath, appFile);
});

debug("reading array of vendor file paths from manifest");
var vendorFiles = manifest.vendorFiles.map(function(vendorFile) {
debug('reading array of vendor file paths from manifest');
let vendorFiles = manifest.vendorFiles.map(function(vendorFile) {
return path.join(distPath, vendorFile);
});

Expand Down Expand Up @@ -468,7 +491,7 @@ function buildBootOptions(shouldRender) {
document: doc,
rootElement,
shouldRender,
_renderMode
_renderMode,
};
}

Expand All @@ -484,10 +507,14 @@ const hasOwnProperty = Object.prototype.hasOwnProperty; // jshint ignore:line

function createShoebox(doc, fastbootInfo) {
let shoebox = fastbootInfo.shoebox;
if (!shoebox) { return; }
if (!shoebox) {
return;
}

for (let key in shoebox) {
if (!hasOwnProperty.call(shoebox, key)) { continue; } // TODO: remove this later #144, ember-fastboot/ember-cli-fastboot/pull/417
if (!hasOwnProperty.call(shoebox, key)) {
continue;
} // TODO: remove this later #144, ember-fastboot/ember-cli-fastboot/pull/417
let value = shoebox[key];
let textValue = JSON.stringify(value);
textValue = escapeJSONString(textValue);
Expand All @@ -507,7 +534,7 @@ const JSON_ESCAPE = {
'>': '\\u003e',
'<': '\\u003c',
'\u2028': '\\u2028',
'\u2029': '\\u2029'
'\u2029': '\\u2029',
};

const JSON_ESCAPE_REGEXP = /[\u2028\u2029&><]/g;
Expand Down
Loading

0 comments on commit 99c6f4d

Please sign in to comment.