Skip to content

Commit

Permalink
feat(npm): updated npm installer
Browse files Browse the repository at this point in the history
Updated release so npm package will be deployed.

closes #192
  • Loading branch information
Christopher Pickering committed Jul 6, 2022
1 parent f688a0b commit 4c0cacc
Show file tree
Hide file tree
Showing 40 changed files with 16,657 additions and 7,343 deletions.
6 changes: 6 additions & 0 deletions .editorconfig
Expand Up @@ -13,3 +13,9 @@ indent_size = 2

[*.js]
indent_size = 2

[*.yml]
indent_size = 2

[*.yaml]
indent_size = 2
34 changes: 34 additions & 0 deletions .github/workflows/test.yml
Expand Up @@ -35,3 +35,37 @@ jobs:
files: ./coverage.xml
fail_ci_if_error: true
verbose: true

test_npm:
name: python ${{ matrix.python-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [3.7, 3.8, 3.9, '3.10']
node: [ 12, 14, 16 ]
fail-fast: true

steps:
- name: checkout
uses: actions/checkout@v3
- name: setup python ${{ matrix.python-version }} on ${{ matrix.os }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: setup node ${{ matrix.node }} on ${{ matrix.os }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}

- name: install project
run: npm --omit=dev install
- name: test post install
run: node ./bin/install.js
- name: test run help
run: node ./bin/index.js -h
- name: test run lint
run: echo "<div>" | node ./bin/index.js
- name: test run check
run: echo "<div>" | node ./bin/index.js --check
7 changes: 7 additions & 0 deletions .prettierignore
@@ -0,0 +1,7 @@
node_modules/*
docs/_site/*
dist/*
.mypy_cache/*
.pytest_cache/*
.tox/*
tests/*
11 changes: 11 additions & 0 deletions .prettierrc
@@ -0,0 +1,11 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": true,
"arrowParens": "always",
"jsxBracketSameLine": false,
"semi": true
}
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -58,6 +58,12 @@ Grab it with `pip`
pip install djlint
```

*Or with the npm experimental install - Note, this requires python and pip to be on your system path.*

```bash
npm i djlint
```

Lint your project

```bash
Expand Down
264 changes: 170 additions & 94 deletions bin/index.js
@@ -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);
});
}
32 changes: 18 additions & 14 deletions bin/install.js
@@ -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);
}

0 comments on commit 4c0cacc

Please sign in to comment.