-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: initial work done to rely on new global config dir * feat: add final functionality to the new global config spec * chore: add comment to tests * fix: typo in comment Co-authored-by: Eduardo Bouças <[email protected]> * fix: don't fail tests if there's no config to backup * fix: make tests resilient to missing config directories * fix: trolled by nodejs fs functions 🤦 * chore: don't delete legacy config and memoise globalConfig result * chore: dropping lodash.once as per #1728 and using memoize-one * chore: dropping lodash.once as per #1728 and using memoize-one * fix: require memoizeOne 🤦 Co-authored-by: Eduardo Bouças <[email protected]>
- Loading branch information
1 parent
b400036
commit 3d8dd28
Showing
16 changed files
with
153 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const Configstore = require('configstore') | ||
const memoizeOne = require('memoize-one') | ||
const { v4: uuidv4 } = require('uuid') | ||
|
||
const { readFileAsync } = require('../lib/fs') | ||
const { getPathInHome, getLegacyPathInHome } = require('../lib/settings') | ||
|
||
const globalConfigDefaults = { | ||
/* disable stats from being sent to Netlify */ | ||
telemetryDisabled: false, | ||
/* cliId */ | ||
cliId: uuidv4(), | ||
} | ||
|
||
const getGlobalConfig = async function () { | ||
const configPath = getPathInHome(['config.json']) | ||
// Legacy config file in home ~/.netlify/config.json | ||
const legacyPath = getLegacyPathInHome(['config.json']) | ||
let legacyConfig | ||
// Read legacy config if exists | ||
try { | ||
legacyConfig = JSON.parse(await readFileAsync(legacyPath)) | ||
} catch (_) {} | ||
// Use legacy config as default values | ||
const defaults = { ...globalConfigDefaults, ...legacyConfig } | ||
const configStore = new Configstore(null, defaults, { configPath }) | ||
|
||
return configStore | ||
} | ||
|
||
// Memoise config result so that we only load it once | ||
module.exports = memoizeOne(getGlobalConfig) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const os = require('os') | ||
const path = require('path') | ||
|
||
const test = require('ava') | ||
|
||
const { | ||
rmdirRecursiveAsync, | ||
mkdirRecursiveAsync, | ||
readFileAsync, | ||
writeFileAsync, | ||
copyFileAsync, | ||
rmFileAsync, | ||
} = require('../lib/fs') | ||
const { getPathInHome, getLegacyPathInHome } = require('../lib/settings') | ||
|
||
const getGlobalConfig = require('./get-global-config.js') | ||
|
||
const configPath = getPathInHome(['config.json']) | ||
const legacyConfigPath = getLegacyPathInHome(['config.json']) | ||
const tmpConfigBackupPath = path.join(os.tmpdir(), `netlify-config-backup-${Date.now()}`) | ||
|
||
test.before('backup current user config if exists', async () => { | ||
try { | ||
await copyFileAsync(configPath, tmpConfigBackupPath) | ||
} catch (_) {} | ||
}) | ||
|
||
test.after.always('cleanup tmp directory and legacy config', async () => { | ||
try { | ||
// Restore user config if exists | ||
await mkdirRecursiveAsync(getPathInHome([])) | ||
await copyFileAsync(tmpConfigBackupPath, configPath) | ||
// Remove tmp backup if exists | ||
await rmFileAsync(tmpConfigBackupPath) | ||
} catch (_) {} | ||
// Remove legacy config path | ||
await rmdirRecursiveAsync(getLegacyPathInHome([])) | ||
}) | ||
|
||
test.beforeEach('recreate clean config directories', async () => { | ||
// Remove config dirs | ||
await rmdirRecursiveAsync(getPathInHome([])) | ||
await rmdirRecursiveAsync(getLegacyPathInHome([])) | ||
// Make config dirs | ||
await mkdirRecursiveAsync(getPathInHome([])) | ||
await mkdirRecursiveAsync(getLegacyPathInHome([])) | ||
}) | ||
|
||
// Not running tests in parallel as we're messing with the same config files | ||
|
||
test.serial('should use legacy config values as default if exists', async (t) => { | ||
const legacyConfig = { someOldKey: 'someOldValue', overrideMe: 'oldValue' } | ||
const newConfig = { overrideMe: 'newValue' } | ||
await writeFileAsync(legacyConfigPath, JSON.stringify(legacyConfig)) | ||
await writeFileAsync(configPath, JSON.stringify(newConfig)) | ||
|
||
const globalConfig = await getGlobalConfig() | ||
t.is(globalConfig.get('someOldKey'), legacyConfig.someOldKey) | ||
t.is(globalConfig.get('overrideMe'), newConfig.overrideMe) | ||
}) | ||
|
||
test.serial('should not throw if legacy config is invalid JSON', async (t) => { | ||
await writeFileAsync(legacyConfigPath, 'NotJson') | ||
await t.notThrowsAsync(getGlobalConfig) | ||
}) | ||
|
||
test.serial("should create config in netlify's config dir if none exists and store new values", async (t) => { | ||
// Remove config dirs | ||
await rmdirRecursiveAsync(getPathInHome([])) | ||
await rmdirRecursiveAsync(getLegacyPathInHome([])) | ||
|
||
const globalConfig = await getGlobalConfig() | ||
globalConfig.set('newProp', 'newValue') | ||
const configFile = JSON.parse(await readFileAsync(configPath)) | ||
t.deepEqual(globalConfig.all, configFile) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters