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

[core] Combine constant values #923

Closed
wants to merge 4 commits into from
Closed
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
9 changes: 5 additions & 4 deletions src/atom-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const StartupTime = require('./startup-time');
const { getReleaseChannel } = require('./get-app-details.js');
const UI = require('./ui.js');
const packagejson = require("../package.json");
const CONSTANTS = require("./pulsar-constants.js");

const stat = util.promisify(fs.stat);

Expand Down Expand Up @@ -393,15 +394,15 @@ class AtomEnvironment {
registerDefaultOpeners() {
this.workspace.addOpener(uri => {
switch (uri) {
case 'atom://.pulsar/stylesheet':
case CONSTANTS.STYLESHEET_URI:
return this.workspace.openTextFile(
this.styles.getUserStyleSheetPath()
);
case 'atom://.pulsar/keymap':
case CONSTANTS.KEYMAP_URI:
return this.workspace.openTextFile(this.keymaps.getUserKeymapPath());
case 'atom://.pulsar/config':
case CONSTANTS.CONFIG_URI:
return this.workspace.openTextFile(this.config.getUserConfigPath());
case 'atom://.pulsar/init-script':
case CONSTANTS.INIT_SCRIPT_URI:
return this.workspace.openTextFile(this.getUserInitScriptPath());
}
});
Expand Down
5 changes: 3 additions & 2 deletions src/atom-paths.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs-plus');
const path = require('path');
const CONSTANTS = require("./pulsar-constants.js");

const hasWriteAccess = dir => {
const testFilePath = path.join(dir, 'write.test');
Expand Down Expand Up @@ -28,7 +29,7 @@ const getAppDirectory = () => {
module.exports = {
setAtomHome: homePath => {
// When a read-writeable .pulsar folder exists above app use that
const portableHomePath = path.join(getAppDirectory(), '..', '.pulsar');
const portableHomePath = path.join(getAppDirectory(), '..', CONSTANTS.DOT_FOLDER);
if (fs.existsSync(portableHomePath)) {
if (hasWriteAccess(portableHomePath)) {
process.env.ATOM_HOME = portableHomePath;
Expand All @@ -46,7 +47,7 @@ module.exports = {
}

// Fall back to default .atom folder in users home folder
process.env.ATOM_HOME = path.join(homePath, '.pulsar');
process.env.ATOM_HOME = path.join(homePath, CONSTANTS.DOT_FOLDER);
},

setUserData: app => {
Expand Down
17 changes: 9 additions & 8 deletions src/main-process/atom-application.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const _ = require('underscore-plus');
let FindParentDir = null;
let Resolve = null;
const ConfigSchema = require('../config-schema');
const CONSTANTS = require("../pulsar-constants.js");

const LocationSuffixRegExp = /(:\d+)(:\d+)?$/;

Expand Down Expand Up @@ -657,21 +658,21 @@ module.exports = class AtomApplication extends EventEmitter {
});
}

this.openPathOnEvent('application:about', 'atom://about');
this.openPathOnEvent('application:show-settings', 'atom://config');
this.openPathOnEvent('application:open-your-config', 'atom://.pulsar/config');
this.openPathOnEvent('application:about', CONSTANTS.ABOUT_UI_URI);
this.openPathOnEvent('application:show-settings', CONSTANTS.CONFIG_UI_URI);
this.openPathOnEvent('application:open-your-config', CONSTANTS.CONFIG_URI);
this.openPathOnEvent(
'application:open-your-init-script',
'atom://.pulsar/init-script'
CONSTANTS.INIT_SCRIPT_URI
);
this.openPathOnEvent('application:open-your-keymap', 'atom://.pulsar/keymap');
this.openPathOnEvent('application:open-your-keymap', CONSTANTS.KEYMAP_URI);
this.openPathOnEvent(
'application:open-your-snippets',
'atom://.pulsar/snippets'
CONSTANTS.SNIPPETS_URI
);
this.openPathOnEvent(
'application:open-your-stylesheet',
'atom://.pulsar/stylesheet'
CONSTANTS.STYLESHEET_URI
);
this.openPathOnEvent(
'application:open-license',
Expand Down Expand Up @@ -1527,7 +1528,7 @@ module.exports = class AtomApplication extends EventEmitter {
// :safeMode - Boolean to control the opened window's safe mode.
openUrl({ urlToOpen, devMode, safeMode, env }) {
const parsedUrl = url.parse(urlToOpen, true);
if (parsedUrl.protocol !== 'atom:') return;
if (parsedUrl.protocol !== CONSTANTS.PROTOCOL_COLON) return;

const pack = this.findPackageWithName(parsedUrl.host, devMode);
if (pack && pack.urlMain) {
Expand Down
3 changes: 2 additions & 1 deletion src/main-process/atom-protocol-handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { protocol } = require('electron');
const fs = require('fs');
const path = require('path');
const CONSTANTS = require("../pulsar-constants.js");

// Handles requests with 'atom' protocol.
//
Expand Down Expand Up @@ -30,7 +31,7 @@ module.exports = class AtomProtocolHandler {

// Creates the 'atom' custom protocol handler.
registerAtomProtocol() {
protocol.registerFileProtocol('atom', (request, callback) => {
protocol.registerFileProtocol(CONSTANTS.PROTOCOL, (request, callback) => {
const relativePath = path.normalize(request.url.substr(7));

let filePath;
Expand Down
11 changes: 6 additions & 5 deletions src/main-process/parse-command-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const dedent = require('dedent');
const yargs = require('yargs');
const { app } = require('electron');
const CONSTANTS = require("../pulsar-constants.js");

module.exports = function parseCommandLine(processArgs) {
// macOS Gatekeeper adds a flag ("-psn_0_[six or seven digits here]") when it intercepts Pulsar launches.
Expand All @@ -28,15 +29,15 @@ module.exports = function parseCommandLine(processArgs) {
A file may be opened at the desired line (and optionally column) by
appending the numbers right after the file name, e.g. \`pulsar file:5:8\`.

Paths that start with \`atom://\` will be interpreted as URLs.
Paths that start with \`${CONSTANTS.PROTOCOL_COLON_SLASHES}\` will be interpreted as URLs.

Environment Variables:

ATOM_DEV_RESOURCE_PATH The path from which Pulsar loads source code in dev mode.
Defaults to \`~/github/atom\`.

ATOM_HOME The root path for all configuration files and folders.
Defaults to \`~/.pulsar\`.`
Defaults to \`~/${CONSTANTS.DOT_FOLDER}\`.`
);
options
.alias('d', 'dev')
Expand Down Expand Up @@ -76,7 +77,7 @@ module.exports = function parseCommandLine(processArgs) {
.boolean('safe')
.describe(
'safe',
'Do not load packages from ~/.pulsar/packages or ~/.pulsar/dev/packages.'
`Do not load packages from ~/${CONSTANTS.DOT_FOLDER}/packages or ~/${CONSTANTS.DOT_FOLDER}/dev/packages.`
);
options
.boolean('benchmark')
Expand Down Expand Up @@ -171,7 +172,7 @@ module.exports = function parseCommandLine(processArgs) {
args = {
uriHandler: true,
'uri-handler': true,
_: args._.filter(str => str.startsWith('atom://')).slice(0, 1)
_: args._.filter(str => str.startsWith(CONSTANTS.PROTOCOL_COLON_SLASHES)).slice(0, 1)
};
}

Expand Down Expand Up @@ -219,7 +220,7 @@ module.exports = function parseCommandLine(processArgs) {
// In the next block, .startsWith() only works on strings. So, skip non-string arguments.
continue;
}
if (path.startsWith('atom://')) {
if (path.startsWith(CONSTANTS.PROTOCOL_COLON_SLASHES)) {
urlsToOpen.push(path);
} else {
pathsToOpen.push(path);
Expand Down
3 changes: 2 additions & 1 deletion src/main-process/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const fs = require('fs');
const CSON = require('season');
const Config = require('../config');
const StartupTime = require('../startup-time');
const CONSTANTS = require("../pulsar-constants.js");

StartupTime.setStartTime();

Expand Down Expand Up @@ -70,7 +71,7 @@ module.exports = function start(resourcePath, devResourcePath, startTime) {
}

const releaseChannel = getReleaseChannel(app.getVersion());
let appUserModelId = 'dev.pulsar-edit.pulsar.' + process.arch;
let appUserModelId = `${CONSTANTS.APP_IDENTIFIER}.${process.arch}`;

// If the release channel is not stable, we append it to the app user model id.
// This allows having the different release channels as separate items in the taskbar.
Expand Down
11 changes: 6 additions & 5 deletions src/protocol-handler-installer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { ipcRenderer } = require('electron');
const CONSTANTS = require("./pulsar-constants.js");

const SETTING = 'core.uriHandlerRegistration';
const PROMPT = 'prompt';
Expand All @@ -12,7 +13,7 @@ module.exports = class ProtocolHandlerInstaller {

async isDefaultProtocolClient() {
return ipcRenderer.invoke('isDefaultProtocolClient', {
protocol: 'atom',
protocol: CONSTANTS.PROTOCOL,
path: process.execPath,
args: ['--uri-handler', '--']
});
Expand All @@ -24,7 +25,7 @@ module.exports = class ProtocolHandlerInstaller {
return (
this.isSupported() &&
ipcRenderer.invoke('setAsDefaultProtocolClient', {
protocol: 'atom',
protocol: CONSTANTS.PROTOCOL,
path: process.execPath,
args: ['--uri-handler', '--']
})
Expand Down Expand Up @@ -82,13 +83,13 @@ module.exports = class ProtocolHandlerInstaller {
};

notification = notifications.addInfo(
'Register as default atom:// URI handler?',
`Register as default ${CONSTANTS.PROTOCOL_COLON_SLASHES} URI handler?`,
{
dismissable: true,
icon: 'link',
description:
'Pulsar is not currently set as the default handler for atom:// URIs. Would you like Pulsar to handle ' +
'atom:// URIs?',
`Pulsar is not currently set as the default handler for ${CONSTANTS.PROTOCOL_COLON_SLASHES} URIs. Would you like Pulsar to handle ` +
`${CONSTANTS.PROTOCOL_COLON_SLASHES} URIs?`,
buttons: [
{
text: 'Yes',
Expand Down
27 changes: 27 additions & 0 deletions src/pulsar-constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// A definition for all Pulsar Constants that are ubiquitous throughout the codebase
const CONSTANTS = {};

CONSTANTS.PROTOCOL = "atom";
CONSTANTS.PROTOCOL_COLON = `${CONSTANTS.PROTOCOL}:`;
CONSTANTS.PROTOCOL_COLON_SLASHES = `${CONSTANTS.PROTOCOL_COLON}//`;
// ^^ atom://
// If changing the protocol name, it must be changed in ./static/index.html
CONSTANTS.DOT_FOLDER = ".pulsar";
// ^^ The folder where all user data and configuration data is stored
CONSTANTS.APP_IDENTIFIER = "dev.pulsar-edit.pulsar";
CONSTANTS.STYLESHEET_URI = `${CONSTANTS.PROTOCOL_COLON_SLASHES}${CONSTANTS.DOT_FOLDER}/stylesheet`;
// ^^ atom://.pulsar/stylesheet
CONSTANTS.KEYMAP_URI = `${CONSTANTS.PROTOCOL_COLON_SLASHES}${CONSTANTS.DOT_FOLDER}/keymap`;
// ^^ atom://.pulsar/keymap
CONSTANTS.CONFIG_URI = `${CONSTANTS.PROTOCOL_COLON_SLASHES}${CONSTANTS.DOT_FOLDER}/config`;
// ^^ atom://.pulsar/config
CONSTANTS.INIT_SCRIPT_URI = `${CONSTANTS.PROTOCOL_COLON_SLASHES}${CONSTANTS.DOT_FOLDER}/init-script`;
// ^^ atom://.pulsar/init-script
CONSTANTS.SNIPPETS_URI = `${CONSTANTS.PROTOCOL_COLON_SLASHES}${CONSTANTS.DOT_FOLDER}/snippets`;
// ^^ atom://.pulsar/snippets
CONSTANTS.CONFIG_UI_URI = `${CONSTANTS.PROTOCOL_COLON_SLASHES}config`;
// ^^ atom://config
CONSTANTS.ABOUT_UI_URI = `${CONSTANTS.PROTOCOL_COLON_SLASHES}about`;
// ^^ atom://about

module.exports = CONSTANTS;
3 changes: 2 additions & 1 deletion src/uri-handler-registry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const url = require('url');
const { Emitter, Disposable } = require('event-kit');
const CONSTANTS = require("./pulsar-constants.js");

// Private: Associates listener functions with URIs from outside the application.
//
Expand Down Expand Up @@ -95,7 +96,7 @@ module.exports = class URIHandlerRegistry {
async handleURI(uri) {
const parsed = url.parse(uri, true);
const { protocol, slashes, auth, port, host } = parsed;
if (protocol !== 'atom:' || slashes !== true || auth || port) {
if (protocol !== CONSTANTS.PROTOCOL_COLON || slashes !== true || auth || port) {
throw new Error(
`URIHandlerRegistry#handleURI asked to handle an invalid URI: ${uri}`
);
Expand Down
3 changes: 2 additions & 1 deletion src/window-event-handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { Disposable, CompositeDisposable } = require('event-kit');
const listen = require('./delegated-listener');
const { debounce } = require('underscore-plus');
const CONSTANTS = require("./pulsar-constants.js");

// Handles low-level events related to the `window`.
module.exports = class WindowEventHandler {
Expand Down Expand Up @@ -306,7 +307,7 @@ module.exports = class WindowEventHandler {
if (uri && uri[0] !== '#') {
if (/^https?:\/\//.test(uri)) {
this.applicationDelegate.openExternal(uri);
} else if (uri.startsWith('atom://')) {
} else if (uri.startsWith(CONSTANTS.PROTOCOL_COLON_SLASHES)) {
this.atomEnvironment.uriHandlerRegistry.handleURI(uri);
}
}
Expand Down
Loading