Skip to content

Commit

Permalink
Merge pull request #16 from cix-cdx/release
Browse files Browse the repository at this point in the history
to master
  • Loading branch information
tpanitte committed Jan 29, 2018
2 parents c9a2e05 + 7e68efd commit 85d0f85
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/HFLicense.process.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const HFLicensor = require('./licensors/HFLicensor');
const HFLicenseProcess = {
assert: (LICProps, callback) => HFLicensor.assert(LICProps.HFLcontent, LICProps.HFLqueries, callback, LICProps.assertAll),
licensing: (LICProps, callback) => HFLicensor.process(LICProps.HFLcontent, LICProps.HFLqueries, callback),
unlicensing: (LICProps, callback) => HFLicensor.unlicense(LICProps.HFLcontent, LICProps.HFLqueries, callback),
};

module.exports = HFLicenseProcess;
49 changes: 48 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,54 @@ const LicensingProcess = {
} else {
console.log(Chalk.yellow('\n - HFLicensing Process : Skipped!\n'));
}
}
};

const UnlicensingProcess = {
LicenseProcess: () => {
console.log(Chalk.cyan('# UNLICENSING PROCESS : Processing..'));

console.log('\n - Process Completed\n');
if (LICProps.proc.indexOf('lic') > -1) {
if (LicenseProcess.unlicensing(LICProps)) {
console.log(Chalk.green('\n License File REMOVED..\n'));
} else {
console.log('\n - License File NOT EXIST..\n');
}
} else {
console.log(Chalk.yellow('\n - Unlicensing Process : Skipped!\n'));
}
},
HFLProcess: () => {
console.log(Chalk.cyan('# UNHFLICENSING PROCESS : Processing..'));

if (LICProps.proc.indexOf('file') > -1) {
const handler = (error, negative, positive) => {
if (negative) {
console.log(` ${negative} - NOT EXIST..`);
} else if (positive) {
console.log(` ${positive} - ${Chalk.green('REMOVED..')} `);
}
};

HFLicenseProcess.unlicensing(LICProps, handler);
} else {
console.log(Chalk.yellow('\n - UnHFLicensing Process : Skipped!\n'));
}
}
};

const outProps = (_licprops, _props) => console.log(`${Chalk.green('#LIC Properties')}
license: ${Chalk.yellow(_licprops.license)}
owner: ${Chalk.green(_licprops.owner)}
beginYear: ${Chalk.green(_licprops.beginYear)}
endYear: ${Chalk.green(_licprops.endYear)}
baseDir: ${Chalk.green(_props.baseDir)}
`);

// :: CLI Process ::

console.log(`\n:: ${Chalk.cyan('CIX-LIC')} ::\n`);
outProps(LICProps, props);

if (LICProps.assert) {
console.log(Chalk.yellow(':: ASSERT MODE ::\n'));
Expand All @@ -96,6 +136,13 @@ if (LICProps.assert) {
console.log();
AssertingProcess.HFLProcess();

} else if (LICProps.unlicense) {
console.log(Chalk.yellow(':: UNLICENSE MODE ::\n'));

UnlicensingProcess.LicenseProcess();
console.log();
UnlicensingProcess.HFLProcess();

} else {
console.log(Chalk.yellow(':: PROCESS MODE ::\n'));

Expand Down
4 changes: 4 additions & 0 deletions lib/core/argv.loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const ArgvLoader = {
}
}

if (v.hasOwnProperty('unlicense')) {
props.unlicense = true;
}

return props;
}
};
Expand Down
1 change: 1 addition & 0 deletions lib/license.process.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Licensor = require('./licensors/licensor');
const LicenseProcess = {
assert: (LICProps) => Licensor.assert(LICProps.licenseFile),
licensing: (LICProps, callback) => Licensor.process(LICProps.licenseContent, LICProps.licenseFile, callback),
unlicensing: (LICProps, callback) => Licensor.unlicense(LICProps.licenseFile, callback),
};

module.exports = LicenseProcess;
29 changes: 29 additions & 0 deletions lib/licensors/HFLicensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,35 @@ const HFLicensor = {
FS.closeSync(fd);

return (content === HFLcontent) || (content.toLowerCase().indexOf(DefaultLICbol) > -1);
},
unlicense: (HFLcontent, filepaths, callback) => {
filepaths.forEach(filepath => {
const length = HFLcontent.length;
const fd = FS.openSync(filepath, 'r');
const data = Buffer.alloc(length);
FS.readSync(fd, data, 0, length);
const content = data.toString(DefaultEncoding);
FS.closeSync(fd);

if (content === HFLcontent) {
const rfname = filepath + DefaultTempExt;
const wfname = filepath;
FS.renameSync(filepath, rfname);

const rs = FS.createReadStream(rfname, { start: length });
const ws = FS.createWriteStream(wfname);
rs.pipe(ws);
FS.unlinkSync(rfname);

if (callback) {
callback(undefined, undefined, filepath);
}
} else {
if (callback) {
callback(undefined, filepath);
}
}
});
}
};

Expand Down
19 changes: 19 additions & 0 deletions lib/licensors/licensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ const Licensor = {
filepath = filepath || DefaultLicenseFile;

return FS.existsSync(filepath);
},
unlicense: (filepath, callback) => {
filepath = filepath || DefaultLicenseFile;

if (FS.existsSync(filepath)) {
FS.unlinkSync(filepath);

if (callback) {
callback(undefined, undefined, filepath);
} else {
return true;
}
} else {
if (callback) {
callback(undefined, filepath);
} else {
return false;
}
}
}
};

Expand Down
4 changes: 4 additions & 0 deletions lib/licprops.builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ const LICPropsBuilder = {
LICProps.assertAll = true;
}

if (props.unlicense) {
LICProps.unlicense = true;
}

return LICProps;
},
buildLicenseProcess: (base, props = {}) => {
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cix-lic",
"version": "2.0.0-alpha.1",
"version": "2.0.0",
"description": "Licensing via CLI (Command-Line), CI (Continuous Integration), and API",
"main": "./lib/index.js",
"bin": {
Expand All @@ -26,16 +26,16 @@
"license": "MIT",
"dependencies": {
"archappenv": "^2.0.0-alpha.51",
"chalk": "^2.3.0",
"mock-require": "^3.0.1"
"chalk": "^2.3.0"
},
"devDependencies": {
"chai": "^4.1.2",
"coveralls": "^3.0.0",
"mocha": "^4.0.1",
"mocha": "^5.0.0",
"mock-require": "^3.0.1",
"nyc": "^11.4.1",
"shelljs": "^0.7.8",
"sinon": "^4.1.3"
"shelljs": "^0.8.1",
"sinon": "^4.2.2"
},
"repository": "git+https://github.com/cix-cdx/cix-lic"
}
18 changes: 18 additions & 0 deletions test/lib/core/argv.loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,23 @@ describe('argv.loader.js tests', () => {
// asserts
expect(result).to.deep.equal(expected);
});

it('expect to load argument values from process, #4', () => {
// arranges
process.argv = [
'', '',
'--unlicense'
];

const expected = {
unlicense: true,
};

// acts
const result = ArgvLoader.load();

// asserts
expect(result).to.deep.equal(expected);
});
});
});
25 changes: 24 additions & 1 deletion test/lib/licprops.builder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ describe('licprops.builder.js tests', () => {

it('expect to build data from props, #4', () => {
// arranges
const json = AppEnv.Util.packageJSON();
const props = {
owner: 'Panit Tuangsuwan',
proc: ['lic', 'file']
Expand All @@ -174,6 +173,30 @@ describe('licprops.builder.js tests', () => {
expect(result).to.deep.equal(expected);
});

it('expect to build data from props, #5', () => {
// arranges
const props = {
owner: 'Panit Tuangsuwan',
unlicense: true,
proc: ['lic', 'file']
};
const expected = {
lic: 'MIT',
license: 'MIT License',
owner: 'Panit Tuangsuwan',
beginYear: '2018',
endYear: 'Present',
unlicense: true,
proc: ['lic', 'file']
};

// acts
const result = LICPropsBuilder.buildBaseData(props);

// asserts
expect(result).to.deep.equal(expected);
});

it('expect to throw an error on strict mode', () => {
// arranges
const json = AppEnv.Util.packageJSON();
Expand Down
1 change: 1 addition & 0 deletions test/resources/files/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// for test
1 change: 1 addition & 0 deletions test/resources/files/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// for test
1 change: 1 addition & 0 deletions test/resources/files/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// for test
1 change: 1 addition & 0 deletions test/resources/files/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// for test

0 comments on commit 85d0f85

Please sign in to comment.