Skip to content

Commit

Permalink
Clean up ComposerService after decaffeinate
Browse files Browse the repository at this point in the history
References #460
  • Loading branch information
Gert-dev committed Jun 2, 2019
1 parent 2e8d050 commit 37a4bce
Showing 1 changed file with 150 additions and 172 deletions.
322 changes: 150 additions & 172 deletions lib/ComposerService.js
Original file line number Diff line number Diff line change
@@ -1,197 +1,175 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS206: Consider reworking classes to avoid initClass
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let ComposerService;
const fs = require('fs');
const path = require('path');
const download = require('download');
const child_process = require('child_process');

module.exports =

//#*
// Handles usage of Composer (PHP package manager).
//#
(ComposerService = (function() {
ComposerService = class ComposerService {
static initClass() {
/**
* The commit to download from the Composer repository.
*
* Currently set to version 1.7.2.
*
* @see https://getcomposer.org/doc/faqs/how-to-install-composer-programmatically.md
*
* @var {String}
*/
this.prototype.COMPOSER_COMMIT = '35ca72b506eba32c0baed4d283a5f834968e5ade';

/**
* @var {Object}
*/
this.prototype.phpInvoker = null;

/**
* @var {String}
*/
this.prototype.folder = null;
}

/**
* @param {Object} phpInvoker
* @param {String} folder
*/
constructor(phpInvoker, folder) {
this.phpInvoker = phpInvoker;
this.folder = folder;
}

/**
* Handles usage of Composer (PHP package manager).
*/
class ComposerService
{
/**
* @param {Object} phpInvoker
* @param {String} folder
*/
constructor(phpInvoker, folder) {
/**
* @param {Array} parameters
* @param {String|null} workingDirectory
* The commit to download from the Composer repository.
*
* @return {Promise}
*/
run(parameters, workingDirectory = null) {
return this.installIfNecessary().then(() => {
const options = {};

if (workingDirectory != null) {
options.cwd = workingDirectory;
}

return new Promise((resolve, reject) => {
const process = this.phpInvoker.invoke([this.getPath()].concat(parameters), [], options);
* Currently set to version 1.7.2.
*
* @see https://getcomposer.org/doc/faqs/how-to-install-composer-programmatically.md
*/
this.COMPOSER_COMMIT = '35ca72b506eba32c0baed4d283a5f834968e5ade';

this.phpInvoker = phpInvoker;
this.folder = folder;
}

/**
* @param {Array} parameters
* @param {String|null} workingDirectory
*
* @return {Promise}
*/
async run(parameters, workingDirectory = null) {
await this.installIfNecessary();

const options = {};

if (workingDirectory != null) {
options.cwd = workingDirectory;
}

process.stdout.on('data', data => {
return console.info('Composer has something to say:', data.toString());
});
return new Promise((resolve, reject) => {
const process = this.phpInvoker.invoke([this.getPath()].concat(parameters), [], options);

process.stderr.on('data', data => {
// Valid information is also sent via STDERR, see also
// https://github.com/composer/composer/issues/3787#issuecomment-76167739
return console.info('Composer has something to say:', data.toString());
});
process.stdout.on('data', data => {
console.info('Composer has something to say:', data.toString());
});

return process.on('close', code => {
console.debug('Composer exited with status code:', code);
process.stderr.on('data', data => {
// Valid information is also sent via STDERR, see also
// https://github.com/composer/composer/issues/3787#issuecomment-76167739
console.info('Composer has something to say:', data.toString());
});

if (code !== 0) {
return reject();
return process.on('close', code => {
console.debug('Composer exited with status code:', code);

} else {
return resolve();
}
});
});
if (code !== 0) {
reject();
} else {
resolve();
}
});
});
}

/**
* @return {Promise}
*/
async installIfNecessary() {
if (this.isInstalled()) {
return new Promise(function(resolve/*, reject*/) {
resolve();
});
}

/**
* @return {Promise}
*/
installIfNecessary() {
if (this.isInstalled()) {
return new Promise(function(resolve, reject) {
return resolve();
});
}

return this.install();
}
await this.install();
}

/**
* @param {Boolean}
*/
isInstalled() {
if (fs.existsSync(this.getPath())) { return true; }
/**
* @param {Boolean}
*/
isInstalled() {
if (fs.existsSync(this.getPath())) {
return true;
}
}

/**
* @return {Promise}
*/
install() {
return this.download().then(() => {
const parameters = [
this.getInstallerFileFilePath(),
`--install-dir=${this.phpInvoker.normalizePlatformAndRuntimePath(this.getInstallerFilePath())}`,
`--filename=${this.getFileName()}`
];

return new Promise((resolve, reject) => {
const process = this.phpInvoker.invoke(parameters);

process.stdout.on('data', data => {
return console.debug('Composer installer has something to say:', data.toString());
});

process.stderr.on('data', data => {
return console.warn('Composer installer has errors to report:', data.toString());
});

return process.on('close', code => {
console.debug('Composer installer exited with status code:', code);

if (code !== 0) {
return reject();

} else {
return resolve();
}
});
});
});
}
/**
* @return {Promise}
*/
async install() {
await this.download();

/**
* @return {Promise}
*/
download() {
return download(
`https://raw.githubusercontent.com/composer/getcomposer.org/${this.COMPOSER_COMMIT}/web/installer`,
this.getInstallerFilePath()
);
}
const parameters = [
this.getInstallerFileFilePath(),
`--install-dir=${this.phpInvoker.normalizePlatformAndRuntimePath(this.getInstallerFilePath())}`,
`--filename=${this.getFileName()}`
];

/**
* @return {String}
*/
getInstallerFilePath() {
return this.folder;
}
return new Promise((resolve, reject) => {
const process = this.phpInvoker.invoke(parameters);

/**
* @return {String}
*/
getInstallerFileFileName() {
return 'installer';
}
process.stdout.on('data', data => {
console.debug('Composer installer has something to say:', data.toString());
});

/**
* @return {String}
*/
getInstallerFileFilePath() {
return this.phpInvoker.normalizePlatformAndRuntimePath(path.join(this.getInstallerFilePath(), this.getInstallerFileFileName()));
}
process.stderr.on('data', data => {
console.warn('Composer installer has errors to report:', data.toString());
});

/**
* @return {String}
*/
getPath() {
return this.phpInvoker.normalizePlatformAndRuntimePath(path.join(this.getInstallerFilePath(), this.getFileName()));
}
return process.on('close', code => {
console.debug('Composer installer exited with status code:', code);

/**
* @return {String}
*/
getFileName() {
return 'composer.phar';
}
};
ComposerService.initClass();
return ComposerService;
})());
if (code !== 0) {
reject();
} else {
resolve();
}
});
});
}

/**
* @return {Promise}
*/
async download() {
await download(
`https://raw.githubusercontent.com/composer/getcomposer.org/${this.COMPOSER_COMMIT}/web/installer`,
this.getInstallerFilePath()
);
}

/**
* @return {String}
*/
getInstallerFilePath() {
return this.folder;
}

/**
* @return {String}
*/
getInstallerFileFileName() {
return 'installer';
}

/**
* @return {String}
*/
getInstallerFileFilePath() {
return this.phpInvoker.normalizePlatformAndRuntimePath(
path.join(this.getInstallerFilePath(), this.getInstallerFileFileName())
);
}

/**
* @return {String}
*/
getPath() {
return this.phpInvoker.normalizePlatformAndRuntimePath(
path.join(this.getInstallerFilePath(), this.getFileName())
);
}

/**
* @return {String}
*/
getFileName() {
return 'composer.phar';
}
};

0 comments on commit 37a4bce

Please sign in to comment.