Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated release so npm package will be deployed. closes #192
- Loading branch information
Christopher Pickering
committed
Jul 6, 2022
1 parent
f688a0b
commit 4c0cacc
Showing
40 changed files
with
16,657 additions
and
7,343 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 |
---|---|---|
|
@@ -13,3 +13,9 @@ indent_size = 2 | |
|
||
[*.js] | ||
indent_size = 2 | ||
|
||
[*.yml] | ||
indent_size = 2 | ||
|
||
[*.yaml] | ||
indent_size = 2 |
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,7 @@ | ||
node_modules/* | ||
docs/_site/* | ||
dist/* | ||
.mypy_cache/* | ||
.pytest_cache/* | ||
.tox/* | ||
tests/* |
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,11 @@ | ||
{ | ||
"printWidth": 80, | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"trailingComma": "all", | ||
"bracketSpacing": true, | ||
"arrowParens": "always", | ||
"jsxBracketSameLine": false, | ||
"semi": 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
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,110 +1,186 @@ | ||
const process = require('process'); | ||
const { PythonShell } = require('python-shell'); | ||
|
||
const {spawn} = require('child_process'); | ||
const yargs = require("yargs"); | ||
const stdin = process.stdin; | ||
|
||
|
||
function getStdin () { | ||
// https://github.com/sindresorhus/get-stdin/pull/19/files | ||
let ret = ''; | ||
|
||
return new Promise(resolve => { | ||
if (stdin.isTTY) { | ||
resolve(ret); | ||
return; | ||
} | ||
|
||
const timeout = setTimeout(() => { | ||
resolve(ret); | ||
}, 100); | ||
PythonShell.defaultOptions = { | ||
mode: 'text', | ||
pythonOptions: ['-u'], | ||
env: { PYCHARM_HOSTED: 1 }, // Force color | ||
}; | ||
|
||
stdin.unref(); | ||
stdin.setEncoding('utf8'); | ||
function clean(output) { | ||
return output.replaceAll('python -m ', ''); | ||
} | ||
|
||
stdin.on('readable', () => { | ||
clearTimeout(timeout); | ||
stdin.ref(); | ||
const yargs = require('yargs'); | ||
|
||
let chunk; | ||
const stdin = process.stdin; | ||
|
||
while ((chunk = stdin.read())) { | ||
ret += chunk; | ||
} | ||
}); | ||
function getStdin() { | ||
// https://github.com/sindresorhus/get-stdin/pull/19/files | ||
let returnValue = ''; | ||
|
||
stdin.on('end', () => { | ||
resolve(ret); | ||
}); | ||
}); | ||
}; | ||
return new Promise((resolve) => { | ||
if (stdin.isTTY) { | ||
resolve(returnValue); | ||
return; | ||
} | ||
|
||
const timeout = setTimeout(() => { | ||
resolve(returnValue); | ||
}, 100); | ||
|
||
getStdin().then(str => {run(str)}) | ||
stdin.unref(); | ||
stdin.setEncoding('utf8'); | ||
|
||
function clean(output){ | ||
return output | ||
.replaceAll("undefined", "") | ||
.replaceAll("python -m djlint", "djlint") | ||
} | ||
stdin.on('readable', () => { | ||
clearTimeout(timeout); | ||
stdin.ref(); | ||
|
||
function run(stdin){ | ||
|
||
var dataToSend; | ||
const exitCode=0; | ||
const options= yargs | ||
.scriptName('djlint') | ||
.usage(`Usage: $0 [OPTIONS] SRC ... | ||
djLint · lint and reformat HTML templates.`) | ||
.option("e", { alias: "extension", describe: "File extension to check [default: html]", type: "string", demandOption: false }) | ||
.option("i", { alias: "ignore", describe: "Codes to ignore. ex: \"H014,H017\"", type: "string", demandOption: false }) | ||
.option("reformat", { describe: "Reformat the file(s).", type: "boolean", demandOption: false }) | ||
.option("check", { describe: "Check formatting on the file(s).", type: "boolean", demandOption: false }) | ||
.option("indent", { describe: "Indent spacing. [default: 4]", type: "int", demandOption: false }) | ||
.option("quiet", { describe: "Do not print diff when reformatting.", type: "boolean", demandOption: false }) | ||
.option("profile", { describe: "Enable defaults by template language. ops: django, jinja, nunjucks, handlebars, golang, angular, html [default: html]", type: "string", demandOption: false }) | ||
.option("require-pragma", { describe: "Only format or lint files that starts with a comment with the text 'djlint:on'", type: "boolean", demandOption: false }) | ||
.option("lint", { describe: "Lint for common issues. [default option]", type: "boolean", demandOption: false }) | ||
.option("use-gitignore", { describe: "Use .gitignore file to extend excludes.", type: "boolean", demandOption: false }) | ||
.argv; | ||
|
||
// set flags | ||
const quiet = options.quiet ? '--quiet' : undefined | ||
const reformat = options.reformat ? '--reformat' : undefined | ||
const check = options.check ? '--check' : undefined | ||
const require_pragma = options["require-pragma"] ? '--require-pragma' : undefined | ||
const lint = options.lint ? '--lint' : undefined | ||
const use_gitignore = options["use-gitignore"] ? '--use-gitignore' : undefined | ||
const has_stdin = stdin !== "" ? "-": options._[0] | ||
|
||
// set variables | ||
const indent = options.indent ? '--indent='+options.indent : undefined | ||
const profile =options.profile ? '--profile='+options.profile : undefined | ||
const ignore = options.ignore ? '--ignore='+options.ignore : undefined | ||
|
||
const args = [has_stdin, quiet,reformat,check,require_pragma,lint,use_gitignore, indent, profile, ignore].filter(x => {return x !== undefined}) | ||
|
||
const python = spawn('python3', ['-m', 'djlint', ...args], {"cwd": "./src"}); | ||
|
||
if(stdin !== ""){ | ||
python.stdin.write(stdin); | ||
python.stdin.end() | ||
} | ||
|
||
python.stdout.on('data', function (data) { | ||
dataToSend += data//.toString(); | ||
}); | ||
let chunk; | ||
|
||
python.stderr.on('data', function (data) { | ||
dataToSend += data//.toString(); | ||
while ((chunk = stdin.read())) { | ||
returnValue += chunk; | ||
} | ||
}); | ||
|
||
python.on('close', (code) => { | ||
process.stdout.write(clean(dataToSend)) | ||
process.exit(code) | ||
stdin.on('end', () => { | ||
resolve(returnValue); | ||
}); | ||
}); | ||
} | ||
|
||
|
||
|
||
|
||
getStdin().then((string_) => { | ||
run(string_); | ||
}); | ||
|
||
function run(stdin) { | ||
const options = yargs | ||
.scriptName('djlint') | ||
.usage( | ||
`Usage: $0 [OPTIONS] SRC ... | ||
djLint · lint and reformat HTML templates.`, | ||
) | ||
.option('e', { | ||
alias: 'extension', | ||
describe: 'File extension to check [default: html]', | ||
type: 'string', | ||
demandOption: false, | ||
}) | ||
.option('h', { | ||
alias: 'help', | ||
describe: 'Show this message and exit.', | ||
type: 'boolean', | ||
demandOption: false, | ||
}) | ||
.option('i', { | ||
alias: 'ignore', | ||
describe: 'Codes to ignore. ex: "H014,H017"', | ||
type: 'string', | ||
demandOption: false, | ||
}) | ||
.option('reformat', { | ||
describe: 'Reformat the file(s).', | ||
type: 'boolean', | ||
demandOption: false, | ||
}) | ||
.option('check', { | ||
describe: 'Check formatting on the file(s).', | ||
type: 'boolean', | ||
demandOption: false, | ||
}) | ||
.option('indent', { | ||
describe: 'Indent spacing. [default: 4]', | ||
type: 'int', | ||
demandOption: false, | ||
}) | ||
.option('quiet', { | ||
describe: 'Do not print diff when reformatting.', | ||
type: 'boolean', | ||
demandOption: false, | ||
}) | ||
.option('warn', { | ||
describe: 'Return errors as warnings.', | ||
type: 'boolean', | ||
demandOption: false, | ||
}) | ||
.option('profile', { | ||
describe: | ||
'Enable defaults by template language. ops: django, jinja, nunjucks, handlebars, golang, angular, html [default: html]', | ||
type: 'string', | ||
demandOption: false, | ||
}) | ||
.option('require-pragma', { | ||
describe: | ||
"Only format or lint files that starts with a comment with the text 'djlint:on'", | ||
type: 'boolean', | ||
demandOption: false, | ||
}) | ||
.option('lint', { | ||
describe: 'Lint for common issues. [default option]', | ||
type: 'boolean', | ||
demandOption: false, | ||
}) | ||
.option('use-gitignore', { | ||
describe: 'Use .gitignore file to extend excludes.', | ||
type: 'boolean', | ||
demandOption: false, | ||
}).argv; | ||
|
||
// Set flags | ||
const quiet = options.quiet ? '--quiet' : undefined; | ||
const help = options.h ? '--help' : undefined; | ||
const warn = options.warn ? '--warn' : undefined; | ||
const reformat = options.reformat ? '--reformat' : undefined; | ||
const check = options.check ? '--check' : undefined; | ||
const require_pragma = options['require-pragma'] | ||
? '--require-pragma' | ||
: undefined; | ||
const lint = options.lint ? '--lint' : undefined; | ||
const use_gitignore = options['use-gitignore'] | ||
? '--use-gitignore' | ||
: undefined; | ||
const has_stdin = stdin === '' ? options._[0] : '-'; | ||
|
||
// Set variables | ||
const indent = options.indent ? '--indent=' + options.indent : undefined; | ||
const profile = options.profile ? '--profile=' + options.profile : undefined; | ||
const ignore = options.ignore ? '--ignore=' + options.ignore : undefined; | ||
const extension = options.e ? '-e=' + options.extension : undefined; | ||
|
||
const args = [ | ||
has_stdin, | ||
warn, | ||
help, | ||
quiet, | ||
extension, | ||
reformat, | ||
check, | ||
require_pragma, | ||
lint, | ||
use_gitignore, | ||
indent, | ||
profile, | ||
ignore, | ||
].filter((x) => { | ||
return x !== undefined; | ||
}); | ||
|
||
const pyshell = new PythonShell('-m', { args: ['djlint', ...args] }); | ||
|
||
if (stdin !== '') { | ||
pyshell.send(stdin); | ||
} | ||
|
||
pyshell.on('message', function (message) { | ||
console.log(clean(message)); | ||
}); | ||
|
||
pyshell.on('stderr', function (message) { | ||
console.log(clean(message)); | ||
}); | ||
|
||
pyshell.end(function (error, code) { | ||
process.exit(code); | ||
}); | ||
} |
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,17 +1,21 @@ | ||
const {spawn} = require('child_process'); | ||
var dataToSend; | ||
const python = spawn('python3', ['-m', 'pip', 'install', '--upgrade','--quiet', '-r', '../requirements.txt'], {"cwd": "./src"}); | ||
const { PythonShell } = require('python-shell'); | ||
|
||
PythonShell.defaultOptions = {}; | ||
const options = { | ||
mode: 'text', | ||
args: ['pip', 'install', 'djlint'], | ||
pythonOptions: ['-u'], | ||
env: { PYCHARM_HOSTED: 1 }, | ||
}; | ||
|
||
python.stdout.on('data', function (data) { | ||
dataToSend += data.toString(); | ||
}); | ||
try { | ||
PythonShell.getVersionSync(); | ||
|
||
python.stderr.on('data', function (data) { | ||
dataToSend += data.toString(); | ||
}); | ||
|
||
python.on('close', (code) => { | ||
process.stdout.write(dataToSend.replace("undefined","")) | ||
process.exit(code) | ||
}); | ||
PythonShell.run('-m', options, function (error, results) { | ||
if (error) throw error; | ||
console.log(results.join('\n')); | ||
}); | ||
} catch (e) { | ||
console.log(e.message); | ||
process.exit(1); | ||
} |
Oops, something went wrong.