Skip to content

Commit 31a3def

Browse files
committed
chore: webpack build system, cli ready!
1 parent 4031443 commit 31a3def

16 files changed

+673
-45
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
node_modules
2-
dist
32
__fixtures__
43
editor-log.txt
5-
cliQuery
4+
cliQuery
5+
dist
6+
babel-dist

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Names:
88

99
❌ Fix bug with `<$>$</$>;` matching too much - JSX text wildcard acts like $$ o.O
1010

11+
❌ restrict more than 2 wildcards on query parse level
12+
1113
⌛ Make CLI a product
1214
- ✅ codeframe from babel
1315
- ✅ investigate results formatting query :`<Text $="ellipsis" ></Text>`
@@ -31,7 +33,7 @@ Names:
3133

3234
✅ Support json
3335

34-
Bundle/minify/obfuscate
36+
Bundle/minify/obfuscate
3537

3638
❌ Invent / Implement license mechanism
3739
- try webassembly

__tests__/search/codePatterns.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('code patterns', () => {
7373
`]
7474

7575
const results = search({
76-
mode: 'include',
76+
mode: 'include', // TODO this should be 'exact', no?
7777
filePaths: filesList,
7878
queryCodes: queries,
7979
})

__tests__/searchMultiThread.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getFilesList } from '/getFilesList'
88
jest.mock('worker_threads', () => {
99
const actual = jest.requireActual('worker_threads')
1010
function Worker(_: string, params: any) {
11-
const mockedPath = path.resolve(process.cwd(), 'dist/src/searchWorker.js')
11+
const mockedPath = path.resolve(process.cwd(), 'dist/worker.js')
1212
return new actual.Worker(mockedPath, params)
1313
}
1414
return {

babel.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ module.exports = {
1111
"rootPathSuffix": "./",
1212
"rootPathPrefix": "/"
1313
}
14-
]
14+
],
15+
"./babel.plugins.js"
1516
],
1617
"ignore": ["dist", "node_modules", "__tests__"]
1718
}

babel.plugins.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
module.exports = function plugin() {
3+
return {
4+
visitor: {
5+
CallExpression(path, state) {
6+
if (['log', 'logStepStart', 'logStepEnd'].includes(path.node.callee?.name) || /^measure/.test(path.node.callee?.name)) {
7+
path.remove()
8+
}
9+
},
10+
VariableDeclaration(path, state) {
11+
if (/^measure/.test(path.node.declarations[0]?.id?.name)) {
12+
path.remove()
13+
14+
}
15+
},
16+
},
17+
};
18+
}

bin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
require('./dist/cli.js')

package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "magic-search",
33
"version": "1.0.0",
44
"description": "AST based search",
5-
"bin": "dist/cli.js",
5+
"bin": "bin.js",
66
"author": "Jakub Mazurek (@jayu) <jakub.mazurek.dev@gmail.com>",
77
"license": "MIT",
88
"devDependencies": {
@@ -31,19 +31,23 @@
3131
"@babel/generator": "7.16.0",
3232
"@babel/parser": "7.16.4",
3333
"@babel/plugin-syntax-typescript": "7.16.0",
34+
"babel-loader": "^8.2.3",
3435
"colorette": "^2.0.16",
3536
"commander": "^8.3.0",
3637
"ignore": "^5.1.9",
3738
"ora": "^6.0.1",
38-
"prettier": "^2.5.1"
39+
"prettier": "^2.5.1",
40+
"webpack": "^5.65.0",
41+
"webpack-cli": "^4.9.1"
3942
},
4043
"scripts": {
41-
"build": "rm -rf dist && babel ./ --extensions \".ts,.tsx\" -d dist",
42-
"build:watch": "babel ./ --extensions \".ts,.tsx\" -d dist --watch",
44+
"build:watch": "webpack --mode development",
45+
"build": "webpack --mode production",
46+
"babel": "rm -rf babel-dist && babel ./ --extensions \".ts,.tsx\" -d babel-dist",
4347
"typecheck": "tsc --noEmit --project tsconfig.json",
4448
"dev": "node ./dist/dev.js",
4549
"cli": "node ./dist/cli.js",
4650
"test": "yarn build && jest --passWithNoTests",
4751
"test:setup": "node ./dist/getFixtures.js"
4852
}
49-
}
53+
}

cli.ts renamed to src/cli.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { getFilesList } from '/getFilesList'
55
import { green, magenta, cyan, bold, red, yellow } from "colorette"
66
import { Mode, getMode, getCodeFrame, print } from '/utils'
77
import { parseQueries } from '/parseQuery'
8-
import { openAsyncEditor } from './terminalEditor'
8+
import { openAsyncEditor } from '/terminalEditor'
99
import { Command } from 'commander';
10-
import ora from 'ora';
10+
import ora from 'ora'
1111
const program = new Command();
1212

1313
program
@@ -28,7 +28,8 @@ program
2828
catch (e) { }
2929

3030
const separator = '\n'.padStart(process.stdout.columns, '━')
31-
const modeAndCaseText = `${separator}${cyan(bold('Mode:'))} ${green(mode)} ${cyan(bold('Case:'))} ${green(caseInsensitive ? 'insensitive' : 'sensitive')}\n`
31+
const rootText = `${cyan(bold('Root:'))} ${green(resolvedRoot)}\n`
32+
const modeAndCaseText = `${separator}${rootText}${cyan(bold('Mode:'))} ${green(mode)} ${cyan(bold('Case:'))} ${green(caseInsensitive ? 'insensitive' : 'sensitive')}\n`
3233
let query = ''
3334

3435
if (queryPath === undefined) {
@@ -63,7 +64,7 @@ program
6364

6465
process.exit(1)
6566
}
66-
const spinner = ora(`Searching ${root}`).start();
67+
const spinner = ora(`Searching`).start();
6768

6869
const results = await search({
6970
mode,
@@ -76,15 +77,16 @@ program
7677

7778
const endTime = Date.now()
7879
if (results.length > 0) {
79-
const first20 = results.slice(0, resultsLimitCount)
80+
const limitedResults = results.slice(0, resultsLimitCount)
8081
const resultsText = results.length <= resultsLimitCount ? `Results:\n` : `First ${resultsLimitCount} results:\n`
8182

8283
print(cyan(bold(resultsText)))
8384

84-
first20.forEach((result) => {
85+
limitedResults.forEach((result) => {
8586
const startLine = result.loc.start.line
8687
const codeFrame = getCodeFrame(result.code, startLine)
87-
print(`${green(result.filePath)}:${magenta(startLine)}:${yellow(result.loc.start.column + 1)}`)
88+
const relativePath = root === process.cwd() ? path.relative(resolvedRoot, result.filePath) : result.filePath
89+
print(`${green(relativePath)}:${magenta(startLine)}:${yellow(result.loc.start.column + 1)}`)
8890
print('\n' + codeFrame + '\n')
8991
})
9092

src/getFilesList.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import ignore from 'ignore';
44
import { measureStart } from './utils';
55

66
export const getFilesList = (root: string) => {
7-
const stop = measureStart('getFiles')
7+
const measureStop = measureStart('getFiles')
88
const ignoreInstance = ignore()
99
const scan = (dir: string): string[] => {
1010
let gitignore = ''
@@ -35,7 +35,7 @@ export const getFilesList = (root: string) => {
3535

3636
const filesList = scan(root)
3737

38-
stop()
38+
measureStop()
3939

4040
return filesList
4141
}

0 commit comments

Comments
 (0)