Skip to content

Commit

Permalink
refactor: fix arrow return formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
erisu committed Dec 18, 2019
1 parent c18da05 commit 08aa0f4
Show file tree
Hide file tree
Showing 8 changed files with 812 additions and 750 deletions.
126 changes: 66 additions & 60 deletions bin/templates/scripts/cordova/lib/check_reqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,54 +82,58 @@ function check_cocoapod_tool (toolChecker) {
* Checks if cocoapods repo size is what is expected
* @return {Promise} Returns a promise either resolved or rejected
*/
module.exports.check_cocoapods_repo_size = () => check_cocoapod_tool()
.then(toolOptions => {
// check size of ~/.cocoapods repo
const commandString = util.format('du -sh %s/.cocoapods', process.env.HOME);
const command = shell.exec(commandString, { silent: true });
// command.output is e.g "750M path/to/.cocoapods", we just scan the number
const size = toolOptions.ignore ? 0 : parseFloat(command.output);

if (toolOptions.ignore || command.code === 0) { // success, parse output
return Q.resolve(size, toolOptions);
} else { // error, perhaps not found
return Q.reject(util.format('%s (%s)', COCOAPODS_REPO_NOT_FOUND_MESSAGE, command.output));
}
})
.then((repoSize, toolOptions) => {
if (toolOptions.ignore || COCOAPODS_SYNCED_MIN_SIZE <= repoSize) { // success, expected size
return Q.resolve(toolOptions);
} else {
return Q.reject(COCOAPODS_SYNC_ERROR_MESSAGE);
}
});
module.exports.check_cocoapods_repo_size = () => {
return check_cocoapod_tool()
.then(toolOptions => {
// check size of ~/.cocoapods repo
const commandString = util.format('du -sh %s/.cocoapods', process.env.HOME);
const command = shell.exec(commandString, { silent: true });
// command.output is e.g "750M path/to/.cocoapods", we just scan the number
const size = toolOptions.ignore ? 0 : parseFloat(command.output);

if (toolOptions.ignore || command.code === 0) { // success, parse output
return Q.resolve(size, toolOptions);
} else { // error, perhaps not found
return Q.reject(util.format('%s (%s)', COCOAPODS_REPO_NOT_FOUND_MESSAGE, command.output));
}
})
.then((repoSize, toolOptions) => {
if (toolOptions.ignore || COCOAPODS_SYNCED_MIN_SIZE <= repoSize) { // success, expected size
return Q.resolve(toolOptions);
} else {
return Q.reject(COCOAPODS_SYNC_ERROR_MESSAGE);
}
});
};

/**
* Checks if cocoapods is available, and whether the repo is synced (because it takes a long time to download)
* @return {Promise} Returns a promise either resolved or rejected
*/
module.exports.check_cocoapods = toolChecker => check_cocoapod_tool(toolChecker)
// check whether the cocoapods repo has been synced through `pod repo` command
// a value of '0 repos' means it hasn't been synced
.then(toolOptions => {
if (toolOptions.ignore) return toolOptions;

// starting with 1.8.0 cocoapods now use cdn and we dont need to sync first
if (versions.compareVersions(toolOptions.version, '1.8.0') >= 0) {
return toolOptions;
}

const code = shell.exec('pod repo | grep -e "^0 repos"', { silent: true }).code;
const repoIsSynced = (code !== 0);

if (repoIsSynced) {
// return check_cocoapods_repo_size();
// we could check the repo size above, but it takes too long.
return toolOptions;
} else {
return Promise.reject(COCOAPODS_NOT_SYNCED_MESSAGE);
}
});
module.exports.check_cocoapods = toolChecker => {
return check_cocoapod_tool(toolChecker)
// check whether the cocoapods repo has been synced through `pod repo` command
// a value of '0 repos' means it hasn't been synced
.then(toolOptions => {
if (toolOptions.ignore) return toolOptions;

// starting with 1.8.0 cocoapods now use cdn and we dont need to sync first
if (versions.compareVersions(toolOptions.version, '1.8.0') >= 0) {
return toolOptions;
}

const code = shell.exec('pod repo | grep -e "^0 repos"', { silent: true }).code;
const repoIsSynced = (code !== 0);

if (repoIsSynced) {
// return check_cocoapods_repo_size();
// we could check the repo size above, but it takes too long.
return toolOptions;
} else {
return Promise.reject(COCOAPODS_NOT_SYNCED_MESSAGE);
}
});
};

/**
* Checks if specific tool is available.
Expand Down Expand Up @@ -198,23 +202,25 @@ module.exports.check_all = () => {
];

// Then execute requirement checks one-by-one
return checkFns.reduce((promise, checkFn, idx) => promise.then(() => {
// If fatal requirement is failed,
// we don't need to check others
if (fatalIsHit) return Q();

const requirement = requirements[idx];
return checkFn()
.then(version => {
requirement.installed = true;
requirement.metadata.version = version;
result.push(requirement);
}, err => {
if (requirement.isFatal) fatalIsHit = true;
requirement.metadata.reason = err;
result.push(requirement);
});
}), Q())
return checkFns.reduce((promise, checkFn, idx) => {
return promise.then(() => {
// If fatal requirement is failed,
// we don't need to check others
if (fatalIsHit) return Q();

const requirement = requirements[idx];
return checkFn()
.then(version => {
requirement.installed = true;
requirement.metadata.version = version;
result.push(requirement);
}, err => {
if (requirement.isFatal) fatalIsHit = true;
requirement.metadata.reason = err;
result.push(requirement);
});
});
}, Q())
// When chain is completed, return requirements array to upstream API
.then(() => result);
};
50 changes: 30 additions & 20 deletions tests/spec/component/versions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,36 @@ const versions = rewire('../../../bin/templates/scripts/cordova/lib/versions');
if (process.platform === 'darwin') {
describe('versions', () => {
describe('get_tool_version method', () => {
it('should not have found tool by name.', () => versions.get_tool_version('unknown').then(
() => fail('expected promise rejection'),
error => expect(error).toContain('is not valid tool name')
));

it('should find xcodebuild version.', () => versions.get_tool_version('xcodebuild').then((version) => {
expect(version).not.toBe(undefined);
}));

it('should find ios-sim version.', () => versions.get_tool_version('ios-sim').then((version) => {
expect(version).not.toBe(undefined);
}));

it('should find ios-deploy version.', () => versions.get_tool_version('ios-deploy').then((version) => {
expect(version).not.toBe(undefined);
}));

it('should find pod version.', () => versions.get_tool_version('pod').then((version) => {
expect(version).not.toBe(undefined);
}));
it('should not have found tool by name.', () => {
return versions.get_tool_version('unknown').then(
() => fail('expected promise rejection'),
error => expect(error).toContain('is not valid tool name')
);
});

it('should find xcodebuild version.', () => {
return versions.get_tool_version('xcodebuild').then((version) => {
expect(version).not.toBe(undefined);
});
});

it('should find ios-sim version.', () => {
return versions.get_tool_version('ios-sim').then((version) => {
expect(version).not.toBe(undefined);
});
});

it('should find ios-deploy version.', () => {
return versions.get_tool_version('ios-deploy').then((version) => {
expect(version).not.toBe(undefined);
});
});

it('should find pod version.', () => {
return versions.get_tool_version('pod').then((version) => {
expect(version).not.toBe(undefined);
});
});
});
});
}
Loading

0 comments on commit 08aa0f4

Please sign in to comment.