Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
LabhanshAgrawal committed Nov 13, 2022
1 parent edf88e5 commit 47bb9be
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 313 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ assignees: ''

- **OS version and name**: <!-- Replace with version + name -->
- **Hyper.app version**: <!-- Replace with version -->
- **Link of a [Gist](https://gist.github.com/) with the contents of your .hyper.js**: <!-- Gist Link Here -->
- **Link of a [Gist](https://gist.github.com/) with the contents of your .hyper.json**: <!-- Gist Link Here -->
- **Relevant information from devtools** _(CMD+ALT+I on macOS, CTRL+SHIFT+I elsewhere)_: <!-- Replace with info if applicable, or N/A -->
- **The issue is reproducible in vanilla Hyper.app**: <!-- Replace with info if applicable, or `Is Vanilla`. (Vanilla means Hyper.app without any add-ons or extras. Straight out of the box.) -->

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ npm-debug.log
yarn-error.log

# optional dev config file and plugins directory
.hyper.js
.hyper.json
.hyper_plugins

.DS_Store
Expand Down
2 changes: 1 addition & 1 deletion PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Prerequisites and steps are described in the ["Contributing" section of our READ
Be sure to use the `canary` branch.

### Create a dev config file
Copy your config file `.hyper.js` to the root of your cloned repository. Hyper, in dev mode, will use this copied config file. That means that you can continue to use your main installation of Hyper with your day-to-day configuration.
Copy your config file `.hyper.json` to the root of your cloned repository. Hyper, in dev mode, will use this copied config file. That means that you can continue to use your main installation of Hyper with your day-to-day configuration.
After the first run, Hyper, in dev mode, will have created a new `.hyper_plugins` directory in your repository directory.

### Setup your plugin
Expand Down
189 changes: 0 additions & 189 deletions app/config/config-default.js

This file was deleted.

69 changes: 17 additions & 52 deletions app/config/import.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {moveSync, copySync, existsSync, writeFileSync, readFileSync, lstatSync} from 'fs-extra';
import {copySync, existsSync, writeFileSync, readFileSync} from 'fs-extra';
import {sync as mkdirpSync} from 'mkdirp';
import {defaultCfg, cfgPath, legacyCfgPath, plugs, defaultPlatformKeyPath} from './paths';
import {_init, _extractDefault} from './init';
import notify from '../notify';
import {rawConfig} from '../../lib/config';
import {parsedConfig, rawConfig} from '../../lib/config';
import _ from 'lodash';

let defaultConfig: rawConfig;

Expand All @@ -18,61 +19,26 @@ const _write = (path: string, data: string) => {
writeFileSync(path, format, 'utf8');
};

// Saves a file as backup by appending '.backup' or '.backup2', '.backup3', etc.
// so as to not override any existing files
const saveAsBackup = (src: string) => {
let attempt = 1;
while (attempt < 100) {
const backupPath = `${src}.backup${attempt === 1 ? '' : attempt}`;
if (!existsSync(backupPath)) {
moveSync(src, backupPath);
return backupPath;
}
attempt++;
}
throw new Error('Failed to create backup for config file. Too many backups');
};

// Migrate Hyper2 config to Hyper3 but only if the user hasn't manually
// Migrate Hyper3 config to Hyper4 but only if the user hasn't manually
// touched the new config and if the old config is not a symlink
const migrateHyper2Config = () => {
if (cfgPath === legacyCfgPath) {
// No need to migrate
const migrateHyper3Config = () => {
if (existsSync(cfgPath)) {
return;
}

if (!existsSync(legacyCfgPath)) {
// Already migrated or user never used Hyper 2
copySync(defaultCfg, cfgPath);
return;
}
const existsNew = existsSync(cfgPath);
if (lstatSync(legacyCfgPath).isSymbolicLink() || (existsNew && lstatSync(cfgPath).isSymbolicLink())) {
// One of the files is a symlink, there could be a number of complications
// in this case so let's avoid those and not do automatic migration
return;
}

if (existsNew) {
const cfg1 = readFileSync(defaultCfg, 'utf8').replace(/\r|\n/g, '');
const cfg2 = readFileSync(cfgPath, 'utf8').replace(/\r|\n/g, '');
const hasNewConfigBeenTouched = cfg1 !== cfg2;
if (hasNewConfigBeenTouched) {
// Assume the user has migrated manually but rename old config to .backup so
// we don't keep trying to migrate on every launch
const backupPath = saveAsBackup(legacyCfgPath);
notify(
'Hyper 3',
`Settings location has changed to ${cfgPath}.\nWe've backed up your old Hyper config to ${backupPath}`
);
return;
}
}

// Migrate
copySync(legacyCfgPath, cfgPath);
saveAsBackup(legacyCfgPath);
const defaultCfgData = JSON.parse(readFileSync(defaultCfg, 'utf8'));
const legacyCfgData = _extractDefault(readFileSync(legacyCfgPath, 'utf8'));
const newCfgData = _.merge(defaultCfgData, legacyCfgData);
_write(cfgPath, JSON.stringify({...newCfgData, test: 'passed'}, null, 2));

notify(
'Hyper 3',
'Hyper 4',
`Settings location has changed to ${cfgPath}.\nWe've automatically migrated your existing config!\nPlease restart Hyper now`
);
};
Expand All @@ -83,18 +49,18 @@ const _importConf = () => {
mkdirpSync(plugs.local);

try {
migrateHyper2Config();
migrateHyper3Config();
} catch (err) {
console.error(err);
}

let defaultCfgRaw = '';
let defaultCfgRaw = '{}';
try {
defaultCfgRaw = readFileSync(defaultCfg, 'utf8');
} catch (err) {
console.log(err);
}
const _defaultCfg = _extractDefault(defaultCfgRaw) as rawConfig;
const _defaultCfg = JSON.parse(defaultCfgRaw);

// Importing platform specific keymap
let content = '{}';
Expand All @@ -121,8 +87,7 @@ const _importConf = () => {
export const _import = () => {
const imported = _importConf();
defaultConfig = imported.defaultCfg;
const result = _init(imported);
return result;
return defaultConfig as parsedConfig;
};

export const getDefaultConfig = () => {
Expand Down
27 changes: 16 additions & 11 deletions app/config/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,29 @@ import {statSync} from 'fs';
import {resolve, join} from 'path';
import isDev from 'electron-is-dev';

const cfgFile = '.hyper.js';
const defaultCfgFile = 'config-default.js';
const cfgFile = '.hyper.json';
const defaultCfgFile = 'config-default.json';
const homeDirectory = homedir();

// If the user defines XDG_CONFIG_HOME they definitely want their config there,
// otherwise use the home directory in linux/mac and userdata in windows
const applicationDirectory =
const legacyCfgPath = join(
process.env.XDG_CONFIG_HOME !== undefined
? join(process.env.XDG_CONFIG_HOME, 'hyper')
: process.platform == 'win32'
? app.getPath('userData')
: homedir();
: homedir(),
'.hyper.js'
);

// If the user defines XDG_CONFIG_HOME they definitely want their config there,
// otherwise use the home directory on non-Windows platforms and AppData on Windows
let cfgDir = join(
process.platform == 'win32'
? app.getPath('userData')
: process.env.XDG_CONFIG_HOME || resolve(homeDirectory, '.config'),
'Hyper'
);

let cfgDir = applicationDirectory;
let cfgPath = join(applicationDirectory, cfgFile);
const legacyCfgPath = join(homeDirectory, cfgFile); // Hyper 2 config location
let cfgPath = join(cfgDir, cfgFile);

const devDir = resolve(__dirname, '../..');
const devCfg = join(devDir, cfgFile);
Expand All @@ -40,8 +47,6 @@ if (isDev) {

const plugins = resolve(cfgDir, '.hyper_plugins');
const plugs = {
legacyBase: resolve(homeDirectory, '.hyper_plugins'),
legacyLocal: resolve(homeDirectory, '.hyper_plugins', 'local'),
base: plugins,
local: resolve(plugins, 'local'),
cache: resolve(plugins, 'cache')
Expand Down
5 changes: 3 additions & 2 deletions app/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {cfgPath} from './config/paths';

// Print diagnostic information for a few arguments instead of running Hyper.
if (['--help', '-v', '--version'].includes(process.argv[1])) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {version} = require('./package');
const configLocation = process.platform === 'win32' ? `${process.env.userprofile}\\.hyper.js` : '~/.hyper.js';
console.log(`Hyper version ${version}`);
console.log('Hyper does not accept any command line arguments. Please modify the config file instead.');
console.log(`Hyper configuration file located at: ${configLocation}`);
console.log(`Hyper configuration file located at: ${cfgPath}`);
process.exit();
}

Expand Down
Loading

0 comments on commit 47bb9be

Please sign in to comment.