Skip to content

Commit

Permalink
This fixes #316 (#634)
Browse files Browse the repository at this point in the history
Introducing a `-pretty` flag to the validate command.
  • Loading branch information
Thels authored Nov 28, 2024
1 parent aac7c0f commit d0fde8d
Show file tree
Hide file tree
Showing 18 changed files with 1,397 additions and 2,965 deletions.
5 changes: 3 additions & 2 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,10 @@ npm run build
# Step 3: Link the workspace locally for testing
npm run link:cli
```
*Currently `watch` based development isn't working - this is in progress and will be resolved seperately.*
# Step 4 : Run `watch` to check for changes automatically and re-bundle. This watching is via `chokidar` and isn't instant - give it a second or two to propogate changes.
npm run watch
```
### CLI Tests
Expand Down
5 changes: 4 additions & 1 deletion cli/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ module.exports = {
testEnvironment: 'node',
testMatch: ['**/*.spec.ts'],
transformIgnorePatterns: [
"<rootDir>/node_modules/(?!@finos/calm-shared)",
'<rootDir>/node_modules/(?!@finos/calm-shared)',
'^.+\\.js$'
],
transform: {
'^.+\\.ts?$': 'ts-jest',
},
rootDir: '.',
watchPathIgnorePatterns: ['<rootDir>/../shared/']
};
9 changes: 5 additions & 4 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"calm": "dist/index.js"
},
"scripts": {
"build": "tsup src/index.ts --format cjs --shims && npm run copy-calm-schema",
"build": "tsup && npm run copy-calm-schema",
"watch": "node watch.mjs",
"copy-calm-schema": "copyfiles \"../calm/draft/2024-04/meta/*\" dist/calm/",
"test": "jest",
"lint": "eslint src",
Expand All @@ -30,8 +31,7 @@
"canvas": "^2.11.2",
"commander": "^12.0.0",
"copyfiles": "^2.4.1",
"mkdirp": "^3.0.1",
"ts-jest": "^29.2.5"
"mkdirp": "^3.0.1"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
Expand All @@ -43,6 +43,7 @@
"@types/xml2js": "^0.4.14",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.15.0",
"chokidar": "^4.0.1",
"eslint": "^9.13.0",
"globals": "^15.12.0",
"jest": "^29.7.0",
Expand All @@ -52,4 +53,4 @@
"typescript": "^5.4.3",
"xml2js": "^0.6.2"
}
}
}
27 changes: 26 additions & 1 deletion cli/src/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('CLI Integration Tests', () => {
let tempDir: string;
const millisPerSecond = 1000;
const integrationTestPrefix = 'calm-test';
const projectRoot = path.resolve(__dirname, '..');
const projectRoot = __dirname;
jest.setTimeout(30 * millisPerSecond);

beforeAll(async () => {
Expand Down Expand Up @@ -135,7 +135,32 @@ describe('CLI Integration Tests', () => {
});
});

test('example validate command - outputting PRETTY to stdout', (done) => {
const exampleValidateCommand = 'calm validate -p ../calm/pattern/api-gateway.json -i ../calm/samples/api-gateway-instantiation.json -f pretty';
exec(exampleValidateCommand, (_error, stdout, _stderr) => {
const expectedFilePath = path.join(__dirname, '../test_fixtures/validate_output_pretty.txt');
const expectedOutput = fs.readFileSync(expectedFilePath, 'utf-8');
//Some minor replacement logic to avoid issues with line endings
expect(stdout.replace(/\r\n/g, '\n')).toEqual(expectedOutput.replace(/\r\n/g, '\n'));
done();
});
});

test('example validate command - outputting PRETTY to file', (done) => {
const targetOutputFile = path.join(tempDir, 'validate-output-pretty.txt');
const exampleValidateCommand = `calm validate -p ../calm/pattern/api-gateway.json -i ../calm/samples/api-gateway-instantiation.json -f pretty -o ${targetOutputFile}`;
exec(exampleValidateCommand, (_error, _stdout, _stderr) => {
expect(fs.existsSync(targetOutputFile)).toBeTruthy();

const outputString = fs.readFileSync(targetOutputFile, 'utf-8');
const expectedFilePath = path.join(__dirname, '../test_fixtures/validate_output_pretty.txt');
const expectedOutput = fs.readFileSync(expectedFilePath, 'utf-8');

//Some minor replacement logic to avoid issues with line endings
expect(outputString.replace(/\r\n/g, '\n')).toEqual(expectedOutput.replace(/\r\n/g, '\n'));
done();
});
});
});


Expand Down
5 changes: 2 additions & 3 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ program
.option(STRICT_OPTION, 'When run in strict mode, the CLI will fail if any warnings are reported.', false)
.addOption(
new Option(FORMAT_OPTION, 'The format of the output')
.choices(['json', 'junit'])
.choices(['json', 'junit', 'pretty'])
.default('json')
)
.option(OUTPUT_OPTION, 'Path location at which to output the generated file.')
Expand All @@ -68,8 +68,7 @@ program
const content = getFormattedOutput(outcome, options.format);
writeOutputFile(options.output, content);
exitBasedOffOfValidationOutcome(outcome, options.strict);
}
);
});

function writeOutputFile(output: string, validationsOutput: string) {
if (output) {
Expand Down
12 changes: 12 additions & 0 deletions cli/test_fixtures/validate_output_pretty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

| Issue Type | Issues Found? | Issue Count |
--------------------------------------------
| Errors | false | 0 |
| Warnings | true | 1 |


Warnings:

| code | severity | message | path | schemaPath | line_start | line_end | character_start | character_end |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| instantiation-has-no-placeholder-properties-numerical | warning | Numerical placeholder (-1) detected in instantiated pattern. | /nodes/2/interfaces/0/port | | 32 | 32 | 18 | 20 |
2 changes: 1 addition & 1 deletion cli/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defineConfig } from 'tsup';

export default defineConfig({
entry: ['src/index.ts'],
format: ['cjs', 'esm'],
format: ['cjs'],
sourcemap: false,
clean: true,
external: ['canvas', 'fsevents', /node_modules/],
Expand Down
45 changes: 45 additions & 0 deletions cli/watch.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* This script uses `chokidar` to watch for changes in the `src` and `shared/dist` directories.
* When a change is detected, it triggers a rebuild of the project by running the `npm run build` command.
*
* The purpose of this script is to automate the build process during development, ensuring that any changes
* to the source files or shared distribution files are quickly reflected in the build output.
*
* The script sets a debounce timeout of 5 seconds to prevent multiple rapid rebuilds in quick succession.
* It also ignores changes to `.map` files to avoid unnecessary rebuilds from compiled JavaScript files.
*
* Usage:
* - Run this script to start watching for changes and automatically rebuild the project when changes are detected.
*/
import { exec } from 'child_process';
import chokidar from 'chokidar';

let timeout;

const runBuild = () => {
if (timeout) clearTimeout(timeout);
timeout = setTimeout(() => {
console.log('Rebuilding...');
exec('npm run build', (err, stdout, stderr) => {
if (err) {
console.error(`Error during build: ${stderr}`);
return;
}
console.log(stdout);
});
}, 5000);
};


const watcher = chokidar.watch(['src', '../shared/dist'], {
persistent: true,
ignoreInitial: true,
ignored: ['**/*.map']
});

watcher.on('all', (event, path) => {
console.log(`File ${path} has been ${event}.`);
runBuild();
});

console.log('Watching for changes in src and shared/dist...');
Loading

0 comments on commit d0fde8d

Please sign in to comment.