Skip to content

Commit

Permalink
Eliminating unnecessary Promise contructors in favor of async
Browse files Browse the repository at this point in the history
All tests still passing. Some unused code and leftover wrapping removed.
  • Loading branch information
Polgár Márton authored and DeeDeeG committed Oct 1, 2023
1 parent 2ba286b commit 0e865d2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 81 deletions.
9 changes: 0 additions & 9 deletions src/apm-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,6 @@ for (let commandClass of commandClasses) {
}
}

function promisifiedRun(commandRun) {
return function(options) {
return new Promise((resolve, _reject) => {
options.callback = resolve;
commandRun.call(this, options);
});
};
}

function parseOptions(args) {
args ??= [];
const options = yargs(args).wrap(Math.min(100, yargs.terminalWidth()));
Expand Down
32 changes: 12 additions & 20 deletions src/disable.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,26 @@ Disables the named package(s).\
return corePackages.concat(devPackages, userPackages);
}

run(options) {
return new Promise((resolve, _reject) => {
let settings;
async run(options) {
options = this.parseOptions(options.commandArgs);

let packageNames = this.packageNamesFromArgv(options.argv);

const configFilePath = CSON.resolve(path.join(config.getAtomDirectory(), 'config'));
if (!configFilePath) {
resolve("Could not find config.cson. Run Atom first?");
return;
return 'Could not find config.cson. Run Atom first?'; //errors as return values atm
}

let settings;
try {
settings = CSON.readFileSync(configFilePath);
} catch (error) {
resolve(`Failed to load \`${configFilePath}\`: ${error.message}`);
return;
return `Failed to load \`${configFilePath}\`: ${error.message}`; //errors as return values atm
}

return void this.getInstalledPackages().then(installedPackages => {


const installedPackageNames = (Array.from(installedPackages).map((pkg) => pkg.name));

// uninstalledPackages = (name for name in packageNames when !installedPackageNames[name])
try {
const installedPackages = await this.getInstalledPackages();
const installedPackageNames = Array.from(installedPackages).map((pkg) => pkg.name);
const uninstalledPackageNames = _.difference(packageNames, installedPackageNames);
if (uninstalledPackageNames.length > 0) {
console.log(`Not Installed:\n ${uninstalledPackageNames.join('\n ')}`);
Expand All @@ -74,8 +68,7 @@ Disables the named package(s).\
packageNames = _.difference(packageNames, uninstalledPackageNames);

if (packageNames.length === 0) {
resolve("Please specify a package to disable");
return;
return "Please specify a package to disable"; //errors as return values atm
}

const keyPath = '*.core.disabledPackages';
Expand All @@ -86,14 +79,13 @@ Disables the named package(s).\
try {
CSON.writeFileSync(configFilePath, settings);
} catch (error) {
resolve(`Failed to save \`${configFilePath}\`: ${error.message}`);
return;
return `Failed to save \`${configFilePath}\`: ${error.message}`; //errors as return values atm
}

console.log(`Disabled:\n ${packageNames.join('\n ')}`);
this.logSuccess();
resolve();
}, error => void resolve(error));
});
} catch (error) {
return error; //errors as return values atm
}
}
}
91 changes: 42 additions & 49 deletions src/enable.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,47 @@ Enables the named package(s).\
return options.alias('h', 'help').describe('help', 'Print this usage message');
}

run(options) {
return new Promise((resolve, _reject) => {
let settings;
options = this.parseOptions(options.commandArgs);
let packageNames = this.packageNamesFromArgv(options.argv);

const configFilePath = CSON.resolve(path.join(config.getAtomDirectory(), 'config'));
if (!configFilePath) {
resolve("Could not find config.cson. Run Atom first?");
return;
}

try {
settings = CSON.readFileSync(configFilePath);
} catch (error) {
resolve(`Failed to load \`${configFilePath}\`: ${error.message}`);
return;
}

const keyPath = '*.core.disabledPackages';
const disabledPackages = _.valueForKeyPath(settings, keyPath) ?? [];

const errorPackages = _.difference(packageNames, disabledPackages);
if (errorPackages.length > 0) {
console.log(`Not Disabled:\n ${errorPackages.join('\n ')}`);
}

// can't enable a package that isn't disabled
packageNames = _.difference(packageNames, errorPackages);

if (packageNames.length === 0) {
resolve("Please specify a package to enable");
return;
}

const result = _.difference(disabledPackages, packageNames);
_.setValueForKeyPath(settings, keyPath, result);

try {
CSON.writeFileSync(configFilePath, settings);
} catch (error) {
resolve(`Failed to save \`${configFilePath}\`: ${error.message}`);
return;
}

console.log(`Enabled:\n ${packageNames.join('\n ')}`);
this.logSuccess();
resolve();
});
async run(options) {
options = this.parseOptions(options.commandArgs);
let packageNames = this.packageNamesFromArgv(options.argv);

const configFilePath = CSON.resolve(path.join(config.getAtomDirectory(), 'config'));
if (!configFilePath) {
return "Could not find config.cson. Run Atom first?"; //errors as retval atm
}

let settings;
try {
settings = CSON.readFileSync(configFilePath);
} catch (error) {
return `Failed to load \`${configFilePath}\`: ${error.message}`; //errors as retval atm
}

const keyPath = '*.core.disabledPackages';
const disabledPackages = _.valueForKeyPath(settings, keyPath) ?? [];

const errorPackages = _.difference(packageNames, disabledPackages);
if (errorPackages.length > 0) {
console.log(`Not Disabled:\n ${errorPackages.join('\n ')}`);
}

// can't enable a package that isn't disabled
packageNames = _.difference(packageNames, errorPackages);

if (packageNames.length === 0) {
return "Please specify a package to enable"; //errors as retval atm
}

const result = _.difference(disabledPackages, packageNames);
_.setValueForKeyPath(settings, keyPath, result);

try {
CSON.writeFileSync(configFilePath, settings);
} catch (error) {
return `Failed to save \`${configFilePath}\`: ${error.message}`; //errors as retval atm
}

console.log(`Enabled:\n ${packageNames.join('\n ')}`);
this.logSuccess();
}
}
6 changes: 3 additions & 3 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ package names to install with optional versions using the
const child = children[0];
const source = path.join(nodeModulesDirectory, child);
const destination = path.join(this.atomPackagesDirectory, child);
commands.push(next => fs.cp(source, destination).then(next, next));
commands.push(next => this.buildModuleCache(pack.name).then(next, next));
commands.push(next => this.warmCompileCache(pack.name).then(next, next));
commands.push(async () => await fs.cp(source, destination));
commands.push(async () => await this.buildModuleCache(pack.name));
commands.push(async () => await this.warmCompileCache(pack.name));

async.waterfall(commands).then(() => {
if (!options.argv.json) { this.logSuccess(); }
Expand Down

0 comments on commit 0e865d2

Please sign in to comment.