-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from jessedc/v2.0.0
version 2.0.0
- Loading branch information
Showing
23 changed files
with
427 additions
and
113 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
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
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
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
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
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,73 @@ | ||
'use strict'; | ||
|
||
var util = require('./util'); | ||
var fs = require('fs'); | ||
var migrate = require('json-schema-migrate'); | ||
var jsonPatch = require('fast-json-patch'); | ||
|
||
|
||
module.exports = { | ||
execute: execute, | ||
schema: { | ||
type: 'object', | ||
required: ['s'], | ||
properties: { | ||
s: { $ref: '#/definitions/stringOrArray' }, | ||
o: { type: 'string' }, | ||
v5: { type: 'boolean' }, | ||
indent: { type: 'integer', minimum: 1 }, | ||
'validate-schema': { type: 'boolean' } | ||
}, | ||
_ajvOptions: false | ||
} | ||
}; | ||
|
||
|
||
function execute(argv) { | ||
var allValid = true; | ||
var opts = { | ||
v5: argv.v5, | ||
validateSchema: argv['validate-schema'] | ||
}; | ||
|
||
var schemaFiles = util.getFiles(argv.s); | ||
if (argv.o && schemaFiles.length > 1) { | ||
console.error('multiple schemas cannot be migrated to a named output file'); | ||
return false; | ||
} | ||
schemaFiles.forEach(migrateSchema); | ||
|
||
return allValid; | ||
|
||
|
||
function migrateSchema(file) { | ||
var schema = util.openFile(file, 'schema ' + file); | ||
var migratedSchema = JSON.parse(JSON.stringify(schema)); | ||
|
||
try { | ||
migrate.draft6(migratedSchema, opts); | ||
var patch = jsonPatch.compare(schema, migratedSchema); | ||
if (patch.length > 0) { | ||
if (argv.o) { | ||
saveSchema(argv.o, migratedSchema); | ||
} else { | ||
var backupFile = file + '.bak'; | ||
fs.writeFileSync(backupFile, fs.readFileSync(file, 'utf8')); | ||
saveSchema(file, migratedSchema); | ||
} | ||
} else { | ||
console.log('no changes in', file); | ||
} | ||
} catch (err) { | ||
allValid = false; | ||
console.error('schema', file, 'is invalid'); | ||
console.error('error:', err.message); | ||
} | ||
} | ||
|
||
|
||
function saveSchema(file, schema) { | ||
fs.writeFileSync(file, JSON.stringify(schema, null, argv.indent || 4)); | ||
console.log('saved migrated schema to', file); | ||
} | ||
} |
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
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
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.