Skip to content

Commit

Permalink
Optimizations and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
kaihaase committed Oct 3, 2024
1 parent f2805e3 commit 05e1896
Show file tree
Hide file tree
Showing 67 changed files with 3,095 additions and 953 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# all files
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

# Markdown files
[*.md]
insert_final_newline = false
trim_trailing_whitespace = false
23 changes: 23 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"extends": "@lenne.tech/ts",
"rules": {
"@typescript-eslint/brace-style": ["error", "1tbs", {"allowSingleLine": false}],
"@typescript-eslint/member-delimiter-style": [
"error",
{
"multiline": {
"delimiter": "semi",
"requireLast": true
},
"singleline": {
"delimiter": "semi",
"requireLast": false
},
"multilineDetection": "brackets"
}
],
"@typescript-eslint/no-require-imports": "off",
"@typescript-eslint/no-var-requires": "off",
"curly": ["error", "all"]
}
}
17 changes: 0 additions & 17 deletions .eslintrc.js

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
steps:
- name: Git checkout
uses: actions/checkout@v4
- name: Use Node.js 16
- name: Use Node.js 20
uses: actions/setup-node@v4
with:
node-version: 16
node-version: 20
- name: Install dependencies
run: npm install
- name: Lint
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
steps:
- name: Git checkout
uses: actions/checkout@v4
- name: Node 16
- name: Node 20
uses: actions/setup-node@v4
with:
node-version: 16
node-version: 20
- name: NPM install
run: npm install
- name: Build
Expand Down
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh

ts-node --skip-project extras/sync-version.ts && npm run lint
ts-node --skip-project extras/sync-version.ts && npm run lint --fix
2 changes: 1 addition & 1 deletion .husky/pre-push
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run lint && npm run test
npm run lint --fix && npm run test
21 changes: 11 additions & 10 deletions __tests__/cli-integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import * as config from '../package.json'
const { system, filesystem } = require('gluegun')
import * as config from '../package.json';

const src = filesystem.path(__dirname, '..')
const { system, filesystem } = require('gluegun');

const src = filesystem.path(__dirname, '..');

const cli = async cmd =>
system.run('node ' + filesystem.path(src, 'bin', 'lt') + ` ${cmd}`)
system.run('node ' + filesystem.path(src, 'bin', 'lt') + ` ${cmd}`);

test('outputs version', async () => {
const output = await cli('--version')
expect(output).toContain(config.version)
})
const output = await cli('--version');
expect(output).toContain(config.version);
});

test('outputs help', async () => {
const output = await cli('--help')
expect(output).toContain(config.version)
})
const output = await cli('--help');
expect(output).toContain(config.version);
});

/*
test('generates file', async () => {
Expand Down
10 changes: 5 additions & 5 deletions bin/lt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

/* tslint:disable */
// check if we're running in dev mode
var devMode = require('fs').existsSync(`${__dirname}/../src`)
var devMode = require('fs').existsSync(`${__dirname}/../src`);
// or want to "force" running the compiled version with --compiled-build
var wantsCompiled = process.argv.indexOf('--compiled-build') >= 0
var wantsCompiled = process.argv.indexOf('--compiled-build') >= 0;

if (wantsCompiled || !devMode) {
// this runs from the compiled javascript source
require(`${__dirname}/../build/cli`).run(process.argv)
require(`${__dirname}/../build/cli`).run(process.argv);
} else {
// this runs from the typescript source (for dev only)
// hook into ts-node so we can run typescript on the fly
require('ts-node').register({ project: `${__dirname}/../tsconfig.json` })
require('ts-node').register({ project: `${__dirname}/../tsconfig.json` });
// run the CLI with the current process arguments
require(`${__dirname}/../src/cli`).run(process.argv)
require(`${__dirname}/../src/cli`).run(process.argv);
}


54 changes: 29 additions & 25 deletions extras/prettier-imports.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { parsers: typescriptParsers } = require('prettier/parser-typescript')
const ts = require('typescript')
const { parsers: typescriptParsers } = require('prettier/parser-typescript');
const ts = require('typescript');

// =============================================================================
// Prettier plugin to optimize and sort imports
Expand All @@ -8,56 +8,60 @@ const ts = require('typescript')

class SingleLanguageServiceHost {
constructor(name, content) {
this.name = name
this.content = content
this.getCompilationSettings = ts.getDefaultCompilerOptions
this.getDefaultLibFileName = ts.getDefaultLibFilePath
this.name = name;
this.content = content;
this.getCompilationSettings = ts.getDefaultCompilerOptions;
this.getDefaultLibFileName = ts.getDefaultLibFilePath;
}

getScriptFileNames() {
return [this.name]
return [this.name];
}

getScriptVersion() {
return ts.version
return ts.version;
}

getScriptSnapshot() {
return ts.ScriptSnapshot.fromString(this.content)
return ts.ScriptSnapshot.fromString(this.content);
}

getCurrentDirectory() {
return ''
return '';
}
}

function applyChanges(text, changes) {
return changes.reduceRight((text, change) => {
const head = text.slice(0, change.span.start)
const tail = text.slice(change.span.start + change.span.length)
return `${head}${change.newText}${tail}`
}, text)
const head = text.slice(0, change.span.start);
const tail = text.slice(change.span.start + change.span.length);
return `${head}${change.newText}${tail}`;
}, text);
}

function organizeImports(text) {
const fileName = 'file.ts'
const host = new SingleLanguageServiceHost(fileName, text)
const languageService = ts.createLanguageService(host)
const formatOptions = ts.getDefaultFormatCodeSettings()
const fileName = 'file.ts';
const host = new SingleLanguageServiceHost(fileName, text);
const languageService = ts.createLanguageService(host);
const formatOptions = ts.getDefaultFormatCodeSettings();
const fileChanges = languageService.organizeImports(
{ type: 'file', fileName },
formatOptions,
{}
)
const textChanges = [...fileChanges.map(change => change.textChanges)]
return applyChanges(text, textChanges)
);
const textChanges = [...fileChanges.map(change => change.textChanges)];
return applyChanges(text, textChanges);
}

const parsers = {
typescript: {
...typescriptParsers.typescript,
preprocess(text) {
text = organizeImports(text)
return text
text = organizeImports(text);
return text;
}
}
}
};

// Uses module.export because of 'Unexpected token export' error
module.exports = parsers
module.exports = parsers;
Loading

0 comments on commit 05e1896

Please sign in to comment.