-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
668 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"javascript.validate.enable": false, | ||
"standard.usePackageJson": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
const dockerfileGenerator = require('../dockerfile-generator'); | ||
const SchemaValidationError = require('../schema-validation-error'); | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const dockerfileGenerator = require('../dockerfile-generator') | ||
const SchemaValidationError = require('../schema-validation-error') | ||
const fs = require('fs-extra') | ||
const path = require('path') | ||
|
||
/* eslint-env jest */ | ||
it('throws schema validation error', async () => { | ||
expect.assertions(1); | ||
const negativeExample = await fs.readJson('./examples/config-negative-example.json'); | ||
await expect(dockerfileGenerator(negativeExample)).rejects.toThrow(SchemaValidationError); | ||
}); | ||
expect.assertions(1) | ||
const negativeExample = await fs.readJson('./examples/config-negative-example.json') | ||
await expect(dockerfileGenerator(negativeExample)).rejects.toThrow(SchemaValidationError) | ||
}) | ||
|
||
it('generates dockerfile successfully', async () => { | ||
expect.assertions(1); | ||
const example = await fs.readJson('./examples/config-example.json'); | ||
const dockerFilePath = path.join(__dirname, '../../out/dockerfile'); | ||
await fs.remove(dockerFilePath); | ||
const result = await dockerfileGenerator(example, dockerFilePath); | ||
expect(result).toEqual(dockerFilePath); | ||
expect(await fs.pathExists(dockerFilePath)).toBeTruthy; | ||
}); | ||
expect.assertions(1) | ||
const example = await fs.readJson('./examples/config-example.json') | ||
const dockerFilePath = path.join(__dirname, '../../out/dockerfile') | ||
await fs.remove(dockerFilePath) | ||
const result = await dockerfileGenerator(example, dockerFilePath) | ||
expect(result).toEqual(dockerFilePath) | ||
expect(await fs.pathExists(dockerFilePath)).toBeTruthy() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
const schemaValidator = require('../schema-validator'); | ||
const fs = require('fs-extra'); | ||
const schemaValidator = require('../schema-validator') | ||
const fs = require('fs-extra') | ||
|
||
/* eslint-env jest */ | ||
it('schema validation succeeds', async () => { | ||
expect.assertions(1); | ||
const schema = await fs.readJson('./schemas/config-schema.json').then(); | ||
const example = await fs.readJson('./examples/config-example.json'); | ||
const validation = await schemaValidator(schema, example); | ||
expect(validation.valid).toBe(true); | ||
}); | ||
expect.assertions(1) | ||
const schema = await fs.readJson('./schemas/config-schema.json').then() | ||
const example = await fs.readJson('./examples/config-example.json') | ||
const validation = await schemaValidator(schema, example) | ||
expect(validation.valid).toBe(true) | ||
}) | ||
|
||
it('schema validation fails', async () => { | ||
expect.assertions(1); | ||
const schema = await fs.readJson('./schemas/config-schema.json').then(); | ||
const negativeExample = await fs.readJson('./examples/config-negative-example.json'); | ||
const validation = await schemaValidator(schema, negativeExample); | ||
expect(validation.valid).toBe(false); | ||
}); | ||
expect.assertions(1) | ||
const schema = await fs.readJson('./schemas/config-schema.json').then() | ||
const negativeExample = await fs.readJson('./examples/config-negative-example.json') | ||
const validation = await schemaValidator(schema, negativeExample) | ||
expect(validation.valid).toBe(false) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,37 @@ | ||
const fs = require('fs-extra'); | ||
const _ = require('lodash'); | ||
const SchemaValidationError = require('./schema-validation-error'); | ||
const schemaValidator = require('./schema-validator'); | ||
const path = require('path'); | ||
const fs = require('fs-extra') | ||
const _ = require('lodash') | ||
const SchemaValidationError = require('./schema-validation-error') | ||
const schemaValidator = require('./schema-validator') | ||
const path = require('path') | ||
|
||
async function configConverter(config) { | ||
let dockerContent = ''; | ||
dockerContent += `From ${config.base_docker}\n`; | ||
if (!_.isNil(config.env_variables)) { | ||
for (const envKey in config.env_variables) { | ||
dockerContent += `ENV ${envKey}=${config.env_variables[envKey]}\n`; | ||
} | ||
async function configConverter (config) { | ||
let dockerContent = '' | ||
dockerContent += `From ${config.base_docker}\n` | ||
if (!_.isNil(config.env_variables)) { | ||
for (const envKey in config.env_variables) { | ||
dockerContent += `ENV ${envKey}=${config.env_variables[envKey]}\n` | ||
} | ||
if (!_.isNil(config.steps)) { | ||
for (buildStep of config.steps) { | ||
if (buildStep.type == 'custom') { | ||
dockerContent += `CMD ${buildStep.config.command}\n`; | ||
} | ||
} | ||
} | ||
if (!_.isNil(config.steps)) { | ||
for (const buildStep of config.steps) { | ||
if (buildStep.type === 'custom') { | ||
dockerContent += `CMD ${buildStep.config.command}\n` | ||
} | ||
} | ||
} | ||
|
||
return dockerContent; | ||
return dockerContent | ||
} | ||
|
||
async function dockerfileGenerator(config, dockerfilePath) { | ||
const configSchema = await fs.readJson(path.join(__dirname, '../schemas/config-schema.json')); | ||
const validation = await schemaValidator(configSchema, config); | ||
if (!validation.valid) { | ||
throw new SchemaValidationError(`The config validation is not passed.`, validation.errors); | ||
} | ||
const dockerContent = await configConverter(config); | ||
await fs.outputFile(dockerfilePath, dockerContent); | ||
return dockerfilePath; | ||
async function dockerfileGenerator (config, dockerfilePath) { | ||
const configSchema = await fs.readJson(path.join(__dirname, '../schemas/config-schema.json')) | ||
const validation = await schemaValidator(configSchema, config) | ||
if (!validation.valid) { | ||
throw new SchemaValidationError(`The config validation is not passed.`, validation.errors) | ||
} | ||
const dockerContent = await configConverter(config) | ||
await fs.outputFile(dockerfilePath, dockerContent) | ||
return dockerfilePath | ||
} | ||
|
||
module.exports = dockerfileGenerator; | ||
module.exports = dockerfileGenerator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
const dockerfileGenerator = require('./dockerfile-generator'); | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const dockerfileGenerator = require('./dockerfile-generator') | ||
const fs = require('fs-extra') | ||
const path = require('path') | ||
|
||
const dockerfilePath = path.join(__dirname, '../out/dockerfile'); | ||
const dockerfilePath = path.join(__dirname, '../out/dockerfile') | ||
fs.readJson('./examples/config-example.json') | ||
.then(example => dockerfileGenerator(example, dockerfilePath)) | ||
.then(data => console.log(data)) | ||
.catch(err => console.log(err)); | ||
.catch(err => console.log(err)) | ||
|
||
module.exports = dockerfileGenerator; | ||
module.exports = dockerfileGenerator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
class SchemaValidationError extends Error { | ||
constructor(message, evalutionDetails) { | ||
super(message); | ||
this.message = message; | ||
this.validationDetails = evalutionDetails; | ||
this.name = 'SchemaValidation'; | ||
} | ||
constructor (message, evalutionDetails) { | ||
super(message) | ||
this.message = message | ||
this.validationDetails = evalutionDetails | ||
this.name = 'SchemaValidation' | ||
} | ||
} | ||
|
||
module.exports = SchemaValidationError; | ||
module.exports = SchemaValidationError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
const Ajv = require('ajv'); | ||
const Ajv = require('ajv') | ||
|
||
async function schemaValidator(schema, data) { | ||
const ajv = new Ajv(); | ||
const valid = ajv.validate(schema, data); | ||
if (!valid) { | ||
return { valid: false, errors: ajv.errors}; | ||
} else { | ||
return { valid: true }; | ||
} | ||
async function schemaValidator (schema, data) { | ||
const ajv = new Ajv() | ||
const valid = ajv.validate(schema, data) | ||
if (!valid) { | ||
return { valid: false, errors: ajv.errors } | ||
} else { | ||
return { valid: true } | ||
} | ||
} | ||
|
||
module.exports = schemaValidator; | ||
module.exports = schemaValidator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.