Skip to content

Commit

Permalink
feat: scan .code-formation/*.cfl scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
NLKNguyen committed Apr 9, 2023
1 parent f283dc5 commit 114b529
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
archive
# Logs
logs
*.log
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ If you want to uninstall:
npm rm -g code-formation
```

<!--
# Publish
```shell
npm publish --access public
```
# Mark release tag
Use IDE to mark tag. Then push to repos:
```shell
git push origin {tag_name}
```
-->

# 📝 Command Line Interface Usage

**code-formation** --scan *[file glob patterns]* --outdir *[base path for output files]*
Expand Down
13 changes: 9 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "code-formation",
"version": "0.6.1",
"version": "0.7.0",
"description": "context-free text manipulator using line-oriented DSL for easy embedding to existing source code",
"main": "./src/index.js",
"bin": "./src/index.js",
Expand All @@ -11,7 +11,7 @@
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
},
"repository": {
"type": "git",
"url": "git+https://github.com/NLKNguyen/code-formation.git"
Expand Down Expand Up @@ -41,8 +41,9 @@
"parse-pairs": "^1.1.0",
"query-string": "^7.1.1",
"requireg": "^0.2.2",
"s-expression.js": "^0.4.0",
"s-expression.js": "^0.6.2",
"vm2": "^3.9.11",
"wildcard-match": "^5.1.2",
"winston": "^3.6.0"
},
"keywords": [
Expand Down
20 changes: 10 additions & 10 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const cheerio = require("cheerio") // jQuery for Node
const chalk = require("chalk") // Colorful console log

const profile = {
marker_prefix: "",
MARKER_PREFIX: "",
variables: {},
snippets: {},
exports: [],
Expand All @@ -36,7 +36,7 @@ const modules = {
marked,
cheerio,
chalk,
"URLSearchParams": URLSearchParams
URLSearchParams: URLSearchParams,
}

invoke = async (snippet, context) => {
Expand Down Expand Up @@ -89,7 +89,7 @@ invoke = async (snippet, context) => {
requireg,
_common: module.exports /* this same module */,
_logger: logger,
_: _
_: _,
},
})
const func = vm.run(script)
Expand Down Expand Up @@ -155,7 +155,7 @@ async function parseParams(str) {
// },
// }

return await S.interpret(S.parse(`(${str})`), {
return await S.interpret(S.parse(str, { includedRootParentheses: false }), {
handlers: {
APPLY: {
evaluate: async (components, context, state, entity) => {
Expand Down Expand Up @@ -284,29 +284,29 @@ function hasTemplateInstruction(profile, line) {
// TODO: snippet block
// TODO: anchor block
const snippetOpenRegex = new RegExp(
`(?<!\`)${profile.marker_prefix}\\$(\\w*)<:([A-Za-z0-9_]+)(.*)`
`(?<!\`)${profile.MARKER_PREFIX}\\$(\\w*)<:([A-Za-z0-9_]+)(.*)`
)

if (snippetOpenRegex.exec(line)) {
return true
}

const snippetCloseRegex = new RegExp(
`(?<!\`)${profile.marker_prefix}\\$(\\w*)>`
`(?<!\`)${profile.MARKER_PREFIX}\\$(\\w*)>`
)
if (snippetCloseRegex.exec(line)) {
return true
}

const blobOpenRegex = new RegExp(
`(?<!\`)${profile.marker_prefix}!(\\w*)<:(.*)`
`(?<!\`)${profile.MARKER_PREFIX}!(\\w*)<:(.*)`
)

if (blobOpenRegex.exec(line)) {
return true
}

const blobCloseRegex = new RegExp(`(?<!\`)${profile.marker_prefix}!(\\w*)>`)
const blobCloseRegex = new RegExp(`(?<!\`)${profile.MARKER_PREFIX}!(\\w*)>`)

if (blobCloseRegex.exec(line)) {
return true
Expand All @@ -318,7 +318,7 @@ function hasTemplateInstruction(profile, line) {
/**
* Check if a path is a local file path, meaning that not have prefix . nor absolute path
* @param {string} path to check if it's a local file path
* @returns
* @returns
*/
function isLocalFilePath(path) {
return path && !/(^\.\/)|(^\.\\)|(^\/)|(^[^:\s]+:)/.test(path)
Expand All @@ -337,5 +337,5 @@ module.exports = {
writeFileSyncRecursive,
isOnlyOneDefined,
hasTemplateInstruction,
isLocalFilePath
isLocalFilePath,
}
12 changes: 6 additions & 6 deletions src/eval-snippet-injections.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function evalBlock(

let line = lines[injection.lineNumber]

let openRegex = openRegexCallback(common.profile.marker_prefix)
let openRegex = openRegexCallback(common.profile.MARKER_PREFIX)
let matched = openRegex.exec(line)
if (!matched) {
return null
Expand All @@ -48,7 +48,7 @@ async function evalBlock(
// console.log(`injection.command = '${injection.command}'`)
// console.log(`injection.snippetInjection = ${injection.snippetInjection}`)
if (closeRegexCallback) {
const closeRegex = closeRegexCallback(common.profile.marker_prefix, label)
const closeRegex = closeRegexCallback(common.profile.MARKER_PREFIX, label)

injection.lineNumber = line_number
while (++injection.lineNumber < lines.length) {
Expand Down Expand Up @@ -95,7 +95,7 @@ async function evalBlock(
const expansion = common.serializeMacro(`(${macro})`)

lines[line_number] = line.replace(openRegex, () => {
return macroExpansionCallback(common.profile.marker_prefix, label, expansion)
return macroExpansionCallback(common.profile.MARKER_PREFIX, label, expansion)
})
logger.info(
`${chalk.cyan(`expand macro "@${snippet_name}":`)} ${chalk.gray(
Expand Down Expand Up @@ -133,15 +133,15 @@ async function evalSnippetInjection(content, params, profile, log) {
const result = []

// const blockSnippetOpenRegex = new RegExp(
// `(?<!\`)${profile.marker_prefix}\\$([A-Za-z0-9_]*)\\[:([@A-Za-z0-9_]+)(\\s*\\(.*\\))?`
// `(?<!\`)${profile.MARKER_PREFIX}\\$([A-Za-z0-9_]*)\\[:([@A-Za-z0-9_]+)(\\s*\\(.*\\))?`
// )

// // const inlineSnippetRegex = new RegExp(
// // `(?<!\`)${profile.marker_prefix}\\$([A-Za-z0-9_]*)!:([@A-Za-z0-9_]+)(\\s+[A-Za-z0-9_]+\\s*=\\s*\".*\")?`
// // `(?<!\`)${profile.MARKER_PREFIX}\\$([A-Za-z0-9_]*)!:([@A-Za-z0-9_]+)(\\s+[A-Za-z0-9_]+\\s*=\\s*\".*\")?`
// // ) // TODO: accept optional label and include that in the macro expansion

// const inlineSnippetRegex = new RegExp(
// `(?<!\`)${profile.marker_prefix}\\$([A-Za-z0-9_]*)!:([@A-Za-z0-9_]+)(\\s*\\(.*\\))?`
// `(?<!\`)${profile.MARKER_PREFIX}\\$([A-Za-z0-9_]*)!:([@A-Za-z0-9_]+)(\\s*\\(.*\\))?`
// )

while (line_number < lines.length) {
Expand Down
23 changes: 21 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const booleanParser = require("boolean")
// const ora = require("ora")
// const xmlescape = require('xml-escape');

const wcmatch = require("wildcard-match")

const scan_scripts = require("./scan-scripts.js")
const scan_snippets = require("./scan-snippets.js")
const scan_blobs = require("./scan-blobs.js")
// const scan_slots = require("./scan-slots.js")
Expand Down Expand Up @@ -64,7 +67,10 @@ const logger = require("./logger.js")
// TODO: --definitions <file> for dotenv file
])

scan = [".code-formation/**", ...scan.split(",").map((e) => e.trim())].filter(Boolean)
scan = [
".code-formation/**",
...scan.split(",").map((e) => e.trim()),
].filter(Boolean)

let sourceFiles = []
for (let p of glob.sync(scan)) {
Expand All @@ -78,7 +84,7 @@ const logger = require("./logger.js")
}
}

// if (!_.isUndefined(define)) {
// if (!_.isUndefined(define)) {
// try {
// define = parsePairs.default(define)
// } catch (e) {
Expand All @@ -93,6 +99,19 @@ const logger = require("./logger.js")
})}`
)

const isCflFile = wcmatch("**/.code-formation/*.cfl")

const cflFiles = sourceFiles.filter(isCflFile)

if (cflFiles.length > 0) {
logger.info(
`${chalk.gray(`detected CFL scripts:`)} ${colorize(cflFiles, {
pretty: true,
})}`
)
await scan_scripts(cflFiles, common.profile, logger)
}

// if (!_.isUndefined(define)) {
// common.profile.variables = define
// }
Expand Down
10 changes: 5 additions & 5 deletions src/scan-blobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ module.exports = async function (files, profile, log) {
let openTag
let doneInterpolation = false
// let openRegex = new RegExp(
// `(?<!\`)${profile.marker_prefix}!([A-Za-z0-9_]*)<:\\s*([@A-Za-z0-9_]+)?(\\s+[A-Za-z0-9_]+\\s*=\\s*\".*\")?`
// `(?<!\`)${profile.MARKER_PREFIX}!([A-Za-z0-9_]*)<:\\s*([@A-Za-z0-9_]+)?(\\s+[A-Za-z0-9_]+\\s*=\\s*\".*\")?`
// )
let openRegex = new RegExp(
`(?<!\`)${profile.marker_prefix}!([A-Za-z0-9_]*)<:\\s*([@A-Za-z0-9_]+)?\\s*(\\(.*\\))?`
`(?<!\`)${profile.MARKER_PREFIX}!([A-Za-z0-9_]*)<:\\s*([@A-Za-z0-9_]+)?\\s*(\\(.*\\))?`
)
// const regex = new RegExp(
// `(?<!\`)${profile.marker_prefix}\\$([A-Za-z0-9_]*)<:([@A-Za-z0-9_]+)\\s*(\\(.*\\))?`
// `(?<!\`)${profile.MARKER_PREFIX}\\$([A-Za-z0-9_]*)<:([@A-Za-z0-9_]+)\\s*(\\(.*\\))?`
// )
while (true) {
openTag = openRegex.exec(line)
Expand Down Expand Up @@ -131,7 +131,7 @@ module.exports = async function (files, profile, log) {
// logger.info(`expansion = ${expansion}`)
lines[line_number] = line.replace(
openRegex,
() => `${profile.marker_prefix}!${label}<:${expansion}`
() => `${profile.MARKER_PREFIX}!${label}<:${expansion}`
)
logger.info(
`${chalk.cyan(
Expand Down Expand Up @@ -252,7 +252,7 @@ module.exports = async function (files, profile, log) {

// const regex = new RegExp(`${focused_entity.tag}>!(.*)`, "g")
const regex = new RegExp(
`(?<!\`)${profile.marker_prefix}!${focused_entity.tag}>`
`(?<!\`)${profile.MARKER_PREFIX}!${focused_entity.tag}>`
)
const closeTag = line.match(regex)
if (closeTag) {
Expand Down
Loading

0 comments on commit 114b529

Please sign in to comment.