Skip to content

Commit 9cb0652

Browse files
committed
initial commit
1 parent 69a1c1b commit 9cb0652

File tree

6 files changed

+150
-0
lines changed

6 files changed

+150
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 OSSPIM
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

git.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const execSync = require('child_process').execSync;
2+
const fs = require('fs-extra');
3+
const path = require('path');
4+
const { logInfo, logError, logDebug } = require('./log');
5+
6+
/**
7+
* Initialises a repository and creates an initial commit. Deletes existing
8+
* .git folder if it already exists and reinitialises the repo.
9+
*
10+
* @param {string} projectPath The path of the project
11+
*/
12+
const initialiseRepository = projectPath => {
13+
logInfo('Initialising git repository');
14+
process.chdir(projectPath);
15+
16+
if (fs.existsSync(path.resolve(projectPath, '.git'))) {
17+
fs.removeSync(path.resolve(projectPath, '.git'));
18+
}
19+
20+
try {
21+
execSync('git init', { stdio: 'ignore' });
22+
execSync('git add -A', { stdio: 'ignore' });
23+
execSync('git commit -m "Initial commit"', { stdio: 'ignore' });
24+
return true;
25+
} catch (err) {
26+
// Perhaps git is not installed
27+
logError('Unable to initialise a git repository');
28+
return false;
29+
}
30+
};
31+
32+
/**
33+
* Clones a repository from the specified git remote.
34+
*
35+
* @param {string} remote The git remote to clone
36+
* @param {string} destination The destination folder
37+
*/
38+
const cloneRepository = (remote, destination) => {
39+
logInfo(`Cloning ${remote}`);
40+
41+
try {
42+
execSync(`git clone --depth 1 ${remote} ${destination}`);
43+
return true;
44+
} catch (err) {
45+
logDebug(err);
46+
47+
// Perhaps git is not installed
48+
logError(`Unable to clone ${remote}`);
49+
return false;
50+
}
51+
};
52+
53+
module.exports = {
54+
initialiseRepository,
55+
cloneRepository
56+
};

index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const git = require('./git');
2+
const log = require('./log');
3+
const node = require('./node');
4+
5+
module.exports = {
6+
...git,
7+
...log,
8+
...node
9+
};

log.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const chalk = require('chalk');
2+
3+
const logDebug = (...parts) => {
4+
if (process.env.DEV) {
5+
console.log(chalk.blue('debug'), ...parts);
6+
}
7+
};
8+
9+
const logError = (...parts) => console.log(chalk.red('error'), ...parts);
10+
const logInfo = (...parts) => console.log(chalk.blue('info'), ...parts);
11+
const logSuccess = (...parts) => console.log(chalk.green('success'), ...parts);
12+
const logWarning = (...parts) => console.log(chalk.yellow('warning'), ...parts);
13+
14+
module.exports = {
15+
logDebug,
16+
logError,
17+
logInfo,
18+
logSuccess,
19+
logWarning
20+
};

node.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const execSync = require('child_process').execSync;
2+
const { logInfo } = require('./log');
3+
4+
/**
5+
* Determines whether yarn is installed.
6+
*/
7+
const shouldUseYarn = () => {
8+
try {
9+
execSync('yarnpkg --version', { stdio: 'ignore' });
10+
return true;
11+
} catch (e) {
12+
return false;
13+
}
14+
};
15+
16+
/**
17+
* Installs Node dependencies using either npm or Yarn.
18+
*
19+
* @param {boolean} useYarn Should Yarn be used for installing dependencies?
20+
* @param {array} dependencies Array of dependencies to install
21+
*/
22+
const installNodeDependencies = useYarn => {
23+
execSync(useYarn ? 'yarnpkg install' : 'npm install', {
24+
stdio: 'inherit'
25+
});
26+
};
27+
28+
module.exports = {
29+
shouldUseYarn,
30+
installNodeDependencies
31+
};

package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@osspim/cli-utils",
3+
"version": "1.0.2",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"chalk": "^2.4.2",
8+
"fs-extra": "^8.1.0"
9+
},
10+
"publishConfig": {
11+
"access": "public"
12+
}
13+
}

0 commit comments

Comments
 (0)