👕 Use standard style for js code

This commit is contained in:
Max Brunsfeld
2015-08-21 16:56:32 -07:00
parent ca4922bb70
commit a4b9b9c6cd
7 changed files with 209 additions and 202 deletions

View File

@@ -21,6 +21,7 @@ module.exports = (grunt) ->
grunt.loadNpmTasks('grunt-babel')
grunt.loadNpmTasks('grunt-coffeelint')
grunt.loadNpmTasks('grunt-lesslint')
grunt.loadNpmTasks('grunt-standard')
grunt.loadNpmTasks('grunt-cson')
grunt.loadNpmTasks('grunt-contrib-csslint')
grunt.loadNpmTasks('grunt-contrib-coffee')
@@ -192,6 +193,12 @@ module.exports = (grunt) ->
'spec/*.coffee'
]
standard:
src: [
'src/**/*.js'
'static/*.js'
]
csslint:
options:
'adjoining-classes': false
@@ -248,7 +255,7 @@ module.exports = (grunt) ->
failOnError: false
grunt.registerTask('compile', ['babel', 'coffee', 'prebuild-less', 'cson', 'peg'])
grunt.registerTask('lint', ['coffeelint', 'csslint', 'lesslint'])
grunt.registerTask('lint', ['standard', 'coffeelint', 'csslint', 'lesslint'])
grunt.registerTask('test', ['shell:kill-atom', 'run-specs'])
ciTasks = ['output-disk-space', 'download-atom-shell', 'download-atom-shell-chromedriver', 'build']

View File

@@ -26,6 +26,7 @@
"grunt-lesslint": "0.17.0",
"grunt-peg": "~1.1.0",
"grunt-shell": "~0.3.1",
"grunt-standard": "^1.0.2",
"harmony-collections": "~0.3.8",
"legal-eagle": "~0.10.0",
"minidump": "~0.9",

View File

@@ -17,14 +17,14 @@ var PREFIX_LENGTH = Math.max.apply(Math, PREFIXES.map(function (prefix) {
return prefix.length
}))
exports.shouldCompile = function(sourceCode) {
exports.shouldCompile = function (sourceCode) {
var start = sourceCode.substr(0, PREFIX_LENGTH)
return PREFIXES.some(function (prefix) {
return start.indexOf(prefix) === 0
})
}
exports.getCachePath = function(sourceCode) {
exports.getCachePath = function (sourceCode) {
if (babelVersionDirectory == null) {
var babelVersion = require('babel-core/package.json').version
babelVersionDirectory = path.join('js', 'babel', createVersionAndOptionsDigest(babelVersion, defaultOptions))
@@ -35,11 +35,11 @@ exports.getCachePath = function(sourceCode) {
crypto
.createHash('sha1')
.update(sourceCode, 'utf8')
.digest('hex') + ".js"
.digest('hex') + '.js'
)
}
exports.compile = function(sourceCode, filePath) {
exports.compile = function (sourceCode, filePath) {
if (!babel) {
babel = require('babel-core')
}

View File

@@ -4,21 +4,21 @@ var crypto = require('crypto')
var path = require('path')
var CoffeeScript = null
exports.shouldCompile = function() {
exports.shouldCompile = function () {
return true
}
exports.getCachePath = function(sourceCode) {
exports.getCachePath = function (sourceCode) {
return path.join(
"coffee",
'coffee',
crypto
.createHash('sha1')
.update(sourceCode, 'utf8')
.digest('hex') + ".js"
.digest('hex') + '.js'
)
}
exports.compile = function(sourceCode, filePath) {
exports.compile = function (sourceCode, filePath) {
if (!CoffeeScript) {
var previousPrepareStackTrace = Error.prepareStackTrace
CoffeeScript = require('coffee-script')

View File

@@ -93,8 +93,9 @@ function writeCachedJavascript (relativeCachePath, code) {
}
function addSourceURL (jsCode, filePath) {
if (process.platform === 'win32')
if (process.platform === 'win32') {
filePath = '/' + path.resolve(filePath).replace(/\\/g, '/')
}
return jsCode + '\n' + '//# sourceURL=' + encodeURI(filePath) + '\n'
}
@@ -114,7 +115,7 @@ require('source-map-support').install({
try {
var sourceCode = fs.readFileSync(filePath, 'utf8')
} catch (error) {
console.warn("Error reading source file", error.stack)
console.warn('Error reading source file', error.stack)
return null
}
@@ -123,7 +124,7 @@ require('source-map-support').install({
try {
var fileData = readCachedJavascript(compiler.getCachePath(sourceCode, filePath))
} catch (error) {
console.warn("Error reading compiled file", error.stack)
console.warn('Error reading compiled file', error.stack)
return null
}
@@ -146,7 +147,7 @@ require('source-map-support').install({
try {
var sourceMap = JSON.parse(new Buffer(rawData, 'base64'))
} catch (error) {
console.warn("Error parsing source map", error.stack)
console.warn('Error parsing source map', error.stack)
return null
}

View File

@@ -13,11 +13,11 @@ var defaultOptions = {
var TypeScriptSimple = null
var typescriptVersionDir = null
exports.shouldCompile = function() {
exports.shouldCompile = function () {
return true
}
exports.getCachePath = function(sourceCode) {
exports.getCachePath = function (sourceCode) {
if (typescriptVersionDir == null) {
var version = require('typescript-simple/package.json').version
typescriptVersionDir = path.join('ts', createVersionAndOptionsDigest(version, defaultOptions))
@@ -32,7 +32,7 @@ exports.getCachePath = function(sourceCode) {
)
}
exports.compile = function(sourceCode, filePath) {
exports.compile = function (sourceCode, filePath) {
if (!TypeScriptSimple) {
TypeScriptSimple = require('typescript-simple').TypeScriptSimple
}

View File

@@ -1,199 +1,197 @@
(function() {
(function () {
var fs = require('fs')
var path = require('path')
var fs = require('fs');
var path = require('path');
var loadSettings = null
var loadSettingsError = null
var loadSettings = null;
var loadSettingsError = null;
window.onload = function() {
try {
var startTime = Date.now();
process.on('unhandledRejection', function(error, promise) {
console.error('Unhandled promise rejection %o with error: %o', promise, error);
});
// Ensure ATOM_HOME is always set before anything else is required
setupAtomHome();
// Normalize to make sure drive letter case is consistent on Windows
process.resourcesPath = path.normalize(process.resourcesPath);
if (loadSettingsError) {
throw loadSettingsError;
}
var devMode = loadSettings.devMode || !loadSettings.resourcePath.startsWith(process.resourcesPath + path.sep);
if (devMode) {
setupDeprecatedPackages();
}
if (loadSettings.profileStartup) {
profileStartup(loadSettings, Date.now() - startTime);
} else {
setupWindow(loadSettings);
setLoadTime(Date.now() - startTime);
}
} catch (error) {
handleSetupError(error);
}
}
var setLoadTime = function(loadTime) {
if (global.atom) {
global.atom.loadTime = loadTime;
console.log('Window load time: ' + global.atom.getWindowLoadTime() + 'ms');
}
}
var handleSetupError = function(error) {
var currentWindow = require('remote').getCurrentWindow();
currentWindow.setSize(800, 600);
currentWindow.center();
currentWindow.show();
currentWindow.openDevTools();
console.error(error.stack || error);
}
var setupWindow = function(loadSettings) {
var compileCache = require('../src/compile-cache')
compileCache.setAtomHomeDirectory(process.env.ATOM_HOME)
ModuleCache = require('../src/module-cache');
ModuleCache.register(loadSettings);
ModuleCache.add(loadSettings.resourcePath);
// Only include deprecated APIs when running core spec
require('grim').includeDeprecatedAPIs = isRunningCoreSpecs(loadSettings);
// Start the crash reporter before anything else.
require('crash-reporter').start({
productName: 'Atom',
companyName: 'GitHub',
// By explicitly passing the app version here, we could save the call
// of "require('remote').require('app').getVersion()".
extra: {_version: loadSettings.appVersion}
});
setupVmCompatibility();
setupCsonCache(compileCache.getCacheDirectory())
require(loadSettings.bootstrapScript);
require('ipc').sendChannel('window-command', 'window:loaded');
}
var setupAtomHome = function() {
if (!process.env.ATOM_HOME) {
var home;
if (process.platform === 'win32') {
home = process.env.USERPROFILE;
} else {
home = process.env.HOME;
}
var atomHome = path.join(home, '.atom');
try {
atomHome = fs.realpathSync(atomHome);
} catch (error) {
// Ignore since the path might just not exist yet.
}
process.env.ATOM_HOME = atomHome;
}
}
var setupCsonCache = function(cacheDir) {
require('season').setCacheDir(path.join(cacheDir, 'cson'));
}
var setupVmCompatibility = function() {
var vm = require('vm');
if (!vm.Script.createContext) {
vm.Script.createContext = vm.createContext;
}
}
var setupDeprecatedPackages = function() {
var metadata = require('../package.json');
if (!metadata._deprecatedPackages) {
try {
metadata._deprecatedPackages = require('../build/deprecated-packages.json');
} catch(requireError) {
console.error('Failed to setup deprecated packages list', requireError.stack);
}
}
}
var profileStartup = function(loadSettings, initialTime) {
var profile = function() {
console.profile('startup');
window.onload = function () {
try {
var startTime = Date.now()
setupWindow(loadSettings);
setLoadTime(Date.now() - startTime + initialTime);
process.on('unhandledRejection', function (error, promise) {
console.error('Unhandled promise rejection %o with error: %o', promise, error)
})
// Ensure ATOM_HOME is always set before anything else is required
setupAtomHome()
// Normalize to make sure drive letter case is consistent on Windows
process.resourcesPath = path.normalize(process.resourcesPath)
if (loadSettingsError) {
throw loadSettingsError
}
var devMode = loadSettings.devMode || !loadSettings.resourcePath.startsWith(process.resourcesPath + path.sep)
if (devMode) {
setupDeprecatedPackages()
}
if (loadSettings.profileStartup) {
profileStartup(loadSettings, Date.now() - startTime)
} else {
setupWindow(loadSettings)
setLoadTime(Date.now() - startTime)
}
} catch (error) {
handleSetupError(error);
} finally {
console.profileEnd('startup');
console.log("Switch to the Profiles tab to view the created startup profile")
handleSetupError(error)
}
};
var currentWindow = require('remote').getCurrentWindow();
if (currentWindow.devToolsWebContents) {
profile();
} else {
currentWindow.openDevTools();
currentWindow.once('devtools-opened', function() {
setTimeout(profile, 100);
});
}
}
var parseLoadSettings = function() {
var rawLoadSettings = decodeURIComponent(location.hash.substr(1));
try {
loadSettings = JSON.parse(rawLoadSettings);
} catch (error) {
console.error("Failed to parse load settings: " + rawLoadSettings);
loadSettingsError = error;
}
}
var setupWindowBackground = function() {
if (loadSettings && loadSettings.isSpec) {
return;
}
var backgroundColor = window.localStorage.getItem('atom:window-background-color');
if (!backgroundColor) {
return;
function setLoadTime (loadTime) {
if (global.atom) {
global.atom.loadTime = loadTime
console.log('Window load time: ' + global.atom.getWindowLoadTime() + 'ms')
}
}
var backgroundStylesheet = document.createElement('style');
backgroundStylesheet.type = 'text/css';
backgroundStylesheet.innerText = 'html, body { background: ' + backgroundColor + '; }';
document.head.appendChild(backgroundStylesheet);
function handleSetupError (error) {
var currentWindow = require('remote').getCurrentWindow()
currentWindow.setSize(800, 600)
currentWindow.center()
currentWindow.show()
currentWindow.openDevTools()
console.error(error.stack || error)
}
// Remove once the page loads
window.addEventListener("load", function loadWindow() {
window.removeEventListener("load", loadWindow, false);
setTimeout(function() {
backgroundStylesheet.remove();
backgroundStylesheet = null;
}, 1000);
}, false);
}
function setupWindow (loadSettings) {
var CompileCache = require('../src/compile-cache')
CompileCache.setAtomHomeDirectory(process.env.ATOM_HOME)
var isRunningCoreSpecs = function(loadSettings) {
return !!(loadSettings &&
loadSettings.isSpec &&
loadSettings.specDirectory &&
loadSettings.resourcePath &&
path.dirname(loadSettings.specDirectory) === loadSettings.resourcePath);
}
var ModuleCache = require('../src/module-cache')
ModuleCache.register(loadSettings)
ModuleCache.add(loadSettings.resourcePath)
parseLoadSettings();
setupWindowBackground();
// Only include deprecated APIs when running core spec
require('grim').includeDeprecatedAPIs = isRunningCoreSpecs(loadSettings)
})();
// Start the crash reporter before anything else.
require('crash-reporter').start({
productName: 'Atom',
companyName: 'GitHub',
// By explicitly passing the app version here, we could save the call
// of "require('remote').require('app').getVersion()".
extra: {_version: loadSettings.appVersion}
})
setupVmCompatibility()
setupCsonCache(CompileCache.getCacheDirectory())
require(loadSettings.bootstrapScript)
require('ipc').sendChannel('window-command', 'window:loaded')
}
function setupAtomHome () {
if (!process.env.ATOM_HOME) {
var home
if (process.platform === 'win32') {
home = process.env.USERPROFILE
} else {
home = process.env.HOME
}
var atomHome = path.join(home, '.atom')
try {
atomHome = fs.realpathSync(atomHome)
} catch (error) {
// Ignore since the path might just not exist yet.
}
process.env.ATOM_HOME = atomHome
}
}
function setupCsonCache (cacheDir) {
require('season').setCacheDir(path.join(cacheDir, 'cson'))
}
function setupVmCompatibility () {
var vm = require('vm')
if (!vm.Script.createContext) {
vm.Script.createContext = vm.createContext
}
}
function setupDeprecatedPackages () {
var metadata = require('../package.json')
if (!metadata._deprecatedPackages) {
try {
metadata._deprecatedPackages = require('../build/deprecated-packages.json')
} catch(requireError) {
console.error('Failed to setup deprecated packages list', requireError.stack)
}
}
}
function profileStartup (loadSettings, initialTime) {
function profile () {
console.profile('startup')
try {
var startTime = Date.now()
setupWindow(loadSettings)
setLoadTime(Date.now() - startTime + initialTime)
} catch (error) {
handleSetupError(error)
} finally {
console.profileEnd('startup')
console.log('Switch to the Profiles tab to view the created startup profile')
}
}
var currentWindow = require('remote').getCurrentWindow()
if (currentWindow.devToolsWebContents) {
profile()
} else {
currentWindow.openDevTools()
currentWindow.once('devtools-opened', function () {
setTimeout(profile, 100)
})
}
}
function parseLoadSettings () {
var rawLoadSettings = decodeURIComponent(window.location.hash.substr(1))
try {
loadSettings = JSON.parse(rawLoadSettings)
} catch (error) {
console.error('Failed to parse load settings: ' + rawLoadSettings)
loadSettingsError = error
}
}
function setupWindowBackground () {
if (loadSettings && loadSettings.isSpec) {
return
}
var backgroundColor = window.localStorage.getItem('atom:window-background-color')
if (!backgroundColor) {
return
}
var backgroundStylesheet = document.createElement('style')
backgroundStylesheet.type = 'text/css'
backgroundStylesheet.innerText = 'html, body { background: ' + backgroundColor + '; }'
document.head.appendChild(backgroundStylesheet)
// Remove once the page loads
window.addEventListener('load', function loadWindow () {
window.removeEventListener('load', loadWindow, false)
setTimeout(function () {
backgroundStylesheet.remove()
backgroundStylesheet = null
}, 1000)
}, false)
}
function isRunningCoreSpecs (loadSettings) {
return !!(loadSettings &&
loadSettings.isSpec &&
loadSettings.specDirectory &&
loadSettings.resourcePath &&
path.dirname(loadSettings.specDirectory) === loadSettings.resourcePath)
}
parseLoadSettings()
setupWindowBackground()
})()