From 2f34c237f358e5653f82e9ab8c8a95c26dbb1d5b Mon Sep 17 00:00:00 2001 From: Shogo Ochiai Date: Fri, 23 Dec 2016 00:35:22 +0700 Subject: [PATCH 1/2] multipule output feature --- README.md | 13 ++++++++++++ lib/swagger-dsl.coffee | 8 +++++-- package.json | 48 ++++++++++++++++++++++++------------------ 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 3a8705b..18b46b7 100644 --- a/README.md +++ b/README.md @@ -237,6 +237,19 @@ will change the extension from `.dsl` to `.json`. More generally, `-r` or `--rename` accepts a comma-delimited regular expression, string pair. The regular expression will be matched against the input filename, and the string will be used to generate the output filename (by replacing `$n` with the text matching *n*th group in the regular expression). +### Multiple output file +Using `-r`, you can generate multiple json file. + +If you have `src/swagger` and `src/swagger2` named DSL, + +```bash +swagger-dsl src/* -r /^src/,\'dest\' +``` + +this command output `dest/swagger` and `dest/swagger2`. + + + ## Licensing diff --git a/lib/swagger-dsl.coffee b/lib/swagger-dsl.coffee index d35bece..244cc04 100755 --- a/lib/swagger-dsl.coffee +++ b/lib/swagger-dsl.coffee @@ -1,4 +1,6 @@ marked = require 'marked' +mkdirp = require 'mkdirp' +pathLib = require 'path' # "private" (not-exported) methods _clone = (a)-> @@ -412,7 +414,7 @@ _main = (argv,logfn,errfn,callback)=> code = "init(this)\n#{data}\nreturn to_json(#{argv.i})\n" json = eval(CoffeeScript.compile(code)) if argv.r? - matches = argv.r.match /^(\/.+\/),((".+\")|(\'.+\'))$/ + matches = argv.r.match /^(\/.+\/),((\".+\")|(\'.+\'))$/ if not matches?[2]? errfn "Error parsing rename pair #{argv.r}" callback(1) @@ -427,7 +429,9 @@ _main = (argv,logfn,errfn,callback)=> if argv.o is '-' logfn json else - fs.writeFileSync(argv.o,json) + mkdirp(pathLib.dirname(argv.o), err -> + fs.writeFileSync(argv.o,json) + ) callback() finally process.argv = original_argv diff --git a/package.json b/package.json index 54ed156..98c989a 100644 --- a/package.json +++ b/package.json @@ -1,32 +1,40 @@ { - "name" : "swagger-dsl", - "version" : "0.2.2", - "description" : "CoffeeScript-based domain-specific language for generating JSON documents for Swagger.", - "repository" : { "type": "git", "url": "https://github.com/intellinote/swagger-dsl.git" }, - "bugs" : { "url": "https://github.com/intellinote/swagger-dsl/issues" }, - "license" : "MIT", - "author" : "Intellinote", + "name": "swagger-dsl", + "version": "0.2.2", + "description": "CoffeeScript-based domain-specific language for generating JSON documents for Swagger.", + "repository": { + "type": "git", + "url": "https://github.com/intellinote/swagger-dsl.git" + }, + "bugs": { + "url": "https://github.com/intellinote/swagger-dsl/issues" + }, + "license": "MIT", + "author": "Intellinote", "dependencies": { - "coffee-script" : "latest", - "optimist" : "latest", - "marked" : "latest" + "coffee-script": "latest", + "marked": "latest", + "mkdirp": "^0.5.1", + "optimist": "latest" }, "devDependencies": { - "browserify" : "latest", - "uglify-js" : "latest", - "coffee-coverage" : "latest", - "docco" : "latest", - "highlight.js" : "latest", - "marked-toc" : "latest", - "mocha" : "latest", - "should" : "latest" + "browserify": "latest", + "uglify-js": "latest", + "coffee-coverage": "latest", + "docco": "latest", + "highlight.js": "latest", + "marked-toc": "latest", + "mocha": "latest", + "should": "latest" }, "main": "lib/swagger-dsl.js", "scripts": { - "test":"./node_modules/.bin/mocha -t 2000 -R list --compilers coffee:coffee-script/register ./test/test-swagger-dsl.coffee" + "test": "./node_modules/.bin/mocha -t 2000 -R list --compilers coffee:coffee-script/register ./test/test-swagger-dsl.coffee" }, "bin": { "swagger-dsl": "./bin/swagger-dsl" }, - "engines": { "node": ">=0.10" } + "engines": { + "node": ">=0.10" + } } From 2664f31e3af4c0ec5a77a2b4b9c262082bfa79f2 Mon Sep 17 00:00:00 2001 From: Shogo Ochiai Date: Fri, 23 Dec 2016 08:59:31 +0700 Subject: [PATCH 2/2] mkdir -p support --- lib/swagger-dsl.coffee | 13 ++++--------- package.json | 1 - 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/swagger-dsl.coffee b/lib/swagger-dsl.coffee index 244cc04..9c1c0a8 100755 --- a/lib/swagger-dsl.coffee +++ b/lib/swagger-dsl.coffee @@ -1,5 +1,5 @@ marked = require 'marked' -mkdirp = require 'mkdirp' +child_process = require 'child_process' pathLib = require 'path' # "private" (not-exported) methods @@ -230,10 +230,6 @@ init = (self,options)-> # **base(path)** sets the document's `basePath` property. this.base = this.basepath = this.base_path = this.basePath = (path)=> rest.basePath = path - - # **resourcePath(path)** sets the document's `resourcePath` property. - this.resourcePath = this.resource_path = this.resourcepath = (path)=> - rest.resourcePath = path # For "convenience", we add variables containing the string version of @@ -414,7 +410,7 @@ _main = (argv,logfn,errfn,callback)=> code = "init(this)\n#{data}\nreturn to_json(#{argv.i})\n" json = eval(CoffeeScript.compile(code)) if argv.r? - matches = argv.r.match /^(\/.+\/),((\".+\")|(\'.+\'))$/ + matches = argv.r.match /^(\/.+\/),((".+\")|(\'.+\'))$/ if not matches?[2]? errfn "Error parsing rename pair #{argv.r}" callback(1) @@ -429,9 +425,8 @@ _main = (argv,logfn,errfn,callback)=> if argv.o is '-' logfn json else - mkdirp(pathLib.dirname(argv.o), err -> - fs.writeFileSync(argv.o,json) - ) + child_process.execSync("mkdir -p #{pathLib.dirname(argv.o)}") + fs.writeFileSync(argv.o,json) callback() finally process.argv = original_argv diff --git a/package.json b/package.json index 98c989a..51a807b 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,6 @@ "dependencies": { "coffee-script": "latest", "marked": "latest", - "mkdirp": "^0.5.1", "optimist": "latest" }, "devDependencies": {