Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Commit

Permalink
Don't rename file if the src and dest are the same (resolves #104) (#105
Browse files Browse the repository at this point in the history
)

Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
  • Loading branch information
Simon Stone authored Apr 29, 2019
1 parent c259909 commit 373066e
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 10 deletions.
21 changes: 14 additions & 7 deletions generators/contract/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,28 @@ module.exports = class extends Generator {
this.options.assetSpaceSeparator = decamelize(this.options.assetCamelCase, ' ');
}

_rename(from, to) {
if (from === to) {
return;
}
this.fs.move(from, to);
}

async writing () {
console.log('Generating files...');
this.fs.copyTpl(this.templatePath(this.options.language), this._getDestination(), this.options, undefined, {globOptions : {dot : true}});
if (this.options.language === 'javascript') {
this.fs.move(this.destinationPath('lib/my-contract.js'), this.destinationPath(`lib/${this.options.assetDashSeparator}-contract.js`));
this.fs.move(this.destinationPath('test/my-contract.js'), this.destinationPath(`test/${this.options.assetDashSeparator}-contract.js`));
this._rename(this.destinationPath('lib/my-contract.js'), this.destinationPath(`lib/${this.options.assetDashSeparator}-contract.js`));
this._rename(this.destinationPath('test/my-contract.js'), this.destinationPath(`test/${this.options.assetDashSeparator}-contract.js`));
}
if (this.options.language === 'typescript') {
this.fs.move(this.destinationPath('src/my-asset.ts'), this.destinationPath(`src/${this.options.assetDashSeparator}.ts`));
this.fs.move(this.destinationPath('src/my-contract.ts'), this.destinationPath(`src/${this.options.assetDashSeparator}-contract.ts`));
this.fs.move(this.destinationPath('src/my-contract.spec.ts'), this.destinationPath(`src/${this.options.assetDashSeparator}-contract.spec.ts`));
this._rename(this.destinationPath('src/my-asset.ts'), this.destinationPath(`src/${this.options.assetDashSeparator}.ts`));
this._rename(this.destinationPath('src/my-contract.ts'), this.destinationPath(`src/${this.options.assetDashSeparator}-contract.ts`));
this._rename(this.destinationPath('src/my-contract.spec.ts'), this.destinationPath(`src/${this.options.assetDashSeparator}-contract.spec.ts`));
}
// npm install does dumb stuff and renames our gitignore to npmignore, so rename it back!
this.fs.move(this.destinationPath('.gitignore-hidefromnpm'), this.destinationPath('.gitignore'));
this.fs.move(this.destinationPath('.npmignore-hidefromnpm'), this.destinationPath('.npmignore'));
this._rename(this.destinationPath('.gitignore-hidefromnpm'), this.destinationPath('.gitignore'));
this._rename(this.destinationPath('.npmignore-hidefromnpm'), this.destinationPath('.npmignore'));
}

async install () {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "generator-fabric",
"version": "0.0.26",
"version": "0.0.27",
"description": "Yeoman generator for Hyperledger Fabric",
"main": "generators/app/index.js",
"scripts": {
Expand Down
128 changes: 127 additions & 1 deletion test/contract/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require('chai').should();

describe('Contract (TypeScript)', () => {

it('should generate a TypeScript project using prompts', async () => {
it('should generate a TypeScript project using prompts (custom asset)', async () => {
let dir;
await helpers.run(path.join(__dirname, '../../generators/app'))
.inTmpDir((dir_) => {
Expand Down Expand Up @@ -138,4 +138,130 @@ describe('Contract (TypeScript)', () => {
});
});

it('should generate a TypeScript project using prompts (default asset)', async () => {
let dir;
await helpers.run(path.join(__dirname, '../../generators/app'))
.inTmpDir((dir_) => {
dir = dir_;
})
.withPrompts({
subgenerator: 'contract',
language: 'typescript',
name: 'my-typescript-contract',
version: '0.0.1',
description: 'My TypeScript Contract',
author: 'James Conga',
license: 'WTFPL',
asset: 'MyAsset'
});
assert.file([
'.vscode/extensions.json',
'.vscode/launch.json',
'src/my-asset.ts',
'src/my-asset-contract.spec.ts',
'src/my-asset-contract.ts',
'src/index.ts',
'.editorconfig',
'.gitignore',
'.npmignore',
'package.json',
'tsconfig.json',
'tslint.json'
]);
assert.fileContent('src/my-asset.ts', /SPDX-License-Identifier: WTFPL/);
assert.fileContent('src/my-asset.ts', /export class MyAsset {/);
assert.fileContent('src/my-asset-contract.ts', /SPDX-License-Identifier: WTFPL/);
assert.fileContent('src/my-asset-contract.ts', /export class MyAssetContract extends Contract {/);
assert.fileContent('src/my-asset-contract.ts', /public async myAssetExists\(ctx: Context, myAssetId: string\): Promise<boolean> {/);
assert.fileContent('src/my-asset-contract.ts', /public async createMyAsset\(ctx: Context, myAssetId: string, value: string\): Promise<void> {/);
assert.fileContent('src/my-asset-contract.ts', /public async readMyAsset\(ctx: Context, myAssetId: string\): Promise<MyAsset> {/);
assert.fileContent('src/my-asset-contract.ts', /public async updateMyAsset\(ctx: Context, myAssetId: string, newValue: string\): Promise<void> {/);
assert.fileContent('src/my-asset-contract.ts', /public async deleteMyAsset\(ctx: Context, myAssetId: string\): Promise<void> {/);
const packageJSON = require(path.join(dir, 'package.json'));
packageJSON.should.deep.equal({
name: 'my-typescript-contract',
version: '0.0.1',
description: 'My TypeScript Contract',
main: 'dist/index.js',
typings: 'dist/index.d.ts',
engines: {
node: '>=8',
npm: '>=5'
},
scripts: {
lint: 'tslint -c tslint.json \'src/**/*.ts\'',
pretest: 'npm run lint',
test: 'nyc mocha -r ts-node/register src/**/*.spec.ts',
start: 'fabric-chaincode-node start',
build: 'tsc',
'build:watch': 'tsc -w',
prepublishOnly: 'npm run build'
},
engineStrict: true,
author: 'James Conga',
license: 'WTFPL',
dependencies: {
'fabric-shim': '1.4.1',
'fabric-contract-api': '1.4.1'
},
devDependencies: {
'@types/chai': '^4.1.7',
'@types/chai-as-promised': '^7.1.0',
'@types/mocha': '^5.2.5',
'@types/node': '^10.12.10',
'@types/sinon': '^5.0.7',
'@types/sinon-chai': '^3.2.1',
chai: '^4.2.0',
'chai-as-promised': '^7.1.1',
mocha: '^5.2.0',
nyc: '^14.0.0',
sinon: '^7.1.1',
'sinon-chai': '^3.3.0',
winston: '^3.2.1',
'ts-node': '^7.0.1',
tslint: '^5.11.0',
typescript: '^3.1.6'
},
nyc: {
extension: [
'.ts',
'.tsx'
],
exclude: [
'coverage/**',
'dist/**'
],
reporter: [
'text-summary',
'html'
],
all: true,
'check-coverage': true,
statements: 100,
branches: 100,
functions: 100,
lines: 100
}
});
const tsconfigJSON = require(path.join(dir, 'tsconfig.json'));
tsconfigJSON.should.deep.equal({
compilerOptions: {
outDir: 'dist',
target: 'es2017',
moduleResolution: 'node',
module: 'commonjs',
declaration: true,
sourceMap: true,
experimentalDecorators: true,
emitDecoratorMetadata: true
},
include: [
'./src/**/*'
],
exclude: [
'./src/**/*.spec.ts'
]
});
});

});

0 comments on commit 373066e

Please sign in to comment.