diff --git a/packages/@textlint/config-inliner/.gitignore b/packages/@textlint/config-inliner/.gitignore new file mode 100644 index 0000000..fbdee75 --- /dev/null +++ b/packages/@textlint/config-inliner/.gitignore @@ -0,0 +1,155 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# next.js build output +.next + +# nuxt.js build output +.nuxt + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + + +### https://raw.github.com/github/gitignore/d2c1bb2b9c72ead618c9f6a48280ebc7a8e0dff6/Global/JetBrains.gitignore + +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + + +# Build files +/lib +/module diff --git a/packages/@textlint/config-inliner/.mocharc.json b/packages/@textlint/config-inliner/.mocharc.json new file mode 100644 index 0000000..b453523 --- /dev/null +++ b/packages/@textlint/config-inliner/.mocharc.json @@ -0,0 +1,5 @@ +{ + "require": [ + "ts-node-test-register" + ] +} \ No newline at end of file diff --git a/packages/@textlint/config-inliner/LICENSE b/packages/@textlint/config-inliner/LICENSE new file mode 100644 index 0000000..887014d --- /dev/null +++ b/packages/@textlint/config-inliner/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2021 azu + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/@textlint/config-inliner/README.md b/packages/@textlint/config-inliner/README.md new file mode 100644 index 0000000..2d8cbb6 --- /dev/null +++ b/packages/@textlint/config-inliner/README.md @@ -0,0 +1,61 @@ +# @textlint/config-inliner + +Inlining config for supporting browser. + +Some rules use `fs`, so `fs` module does not work in a browser. +Instead of using `fs`, inlining the content before compiling by `@textlint/script-compiler`. + +[textlint-scripts build](https://github.com/textlint/textlint-scripts#textlint-scripts-build) inlining `fs` usage by default. + +`@textlint/config-inliner`'s target is `.textlintrc` configration file. + +- [ ] TODO: we need to defined common rule for inlining. + +## Supported Rules + +- [x] [textlint-rule-prh](https://github.com/textlint-rule/textlint-rule-prh) + +## Install + +Install with [npm](https://www.npmjs.com/): + + npm install @textlint/config-inliner + +## Usage + +```ts +import { loadConfig } from "@textlint/config-loader"; +import { inlineConfig } from "@textlint/config-inliner"; +const config = await loadConfig(); +const inlinedConfig = await inlineConfig(config); +``` + +## Changelog + +See [Releases page](https://github.com/textlint/editor/releases). + +## Running tests + +Install devDependencies and Run `npm test`: + + npm test + +## Contributing + +Pull requests and stars are always welcome. + +For bugs and feature requests, [please create an issue](https://github.com/textlint/editor/issues). + +1. Fork it! +2. Create your feature branch: `git checkout -b my-new-feature` +3. Commit your changes: `git commit -am 'Add some feature'` +4. Push to the branch: `git push origin my-new-feature` +5. Submit a pull request :D + +## Author + +- azu: [GitHub](https://github.com/azu), [Twitter](https://twitter.com/azu_re) + +## License + +MIT © azu diff --git a/packages/@textlint/config-inliner/package.json b/packages/@textlint/config-inliner/package.json new file mode 100644 index 0000000..53d76b2 --- /dev/null +++ b/packages/@textlint/config-inliner/package.json @@ -0,0 +1,63 @@ +{ + "name": "@textlint/config-inliner", + "version": "0.11.0", + "description": "Inlining config for supporting browser.", + "keywords": [ + "textlint" + ], + "homepage": "https://github.com/textlint/editor/tree/master/packages/@textlint/config-inliner/", + "bugs": { + "url": "https://github.com/textlint/editor/issues" + }, + "repository": { + "type": "git", + "url": "https://github.com/textlint/editor.git" + }, + "license": "MIT", + "author": "azu", + "sideEffects": false, + "main": "lib/config-inliner.js", + "module": "module/config-inliner.js", + "types": "lib/config-inliner.d.ts", + "directories": { + "lib": "lib", + "test": "test" + }, + "files": [ + "bin/", + "lib/", + "module/", + "src/" + ], + "scripts": { + "test": "mocha \"test/**/*.ts\"", + "format": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"", + "build": "tsc -p . && tsc -p ./tsconfig.module.json", + "watch": "tsc -p . --watch", + "clean": "rimraf lib/ module/", + "prepublishOnly": "npm run clean && npm run build" + }, + "prettier": { + "printWidth": 120, + "singleQuote": false, + "tabWidth": 4, + "trailingComma": "none" + }, + "dependencies": { + "untildify": "^4.0.0", + "@textlint/config-loader": "^0.11.0" + }, + "devDependencies": { + "@types/mocha": "^8.2.2", + "@types/node": "^15.3.0", + "mocha": "^8.4.0", + "prettier": "^2.3.0", + "rimraf": "^3.0.2", + "ts-node": "^9.1.1", + "ts-node-test-register": "^9.0.1", + "typescript": "^4.2.4" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/@textlint/config-inliner/src/config-inliner.ts b/packages/@textlint/config-inliner/src/config-inliner.ts new file mode 100644 index 0000000..dacf51b --- /dev/null +++ b/packages/@textlint/config-inliner/src/config-inliner.ts @@ -0,0 +1,19 @@ +import type { TextlintConfigDescriptor } from "@textlint/config-loader"; +import { prh } from "./rules/prh"; + +export const inlineConfig = async (config: TextlintConfigDescriptor): Promise => { + return { + ...config, + rules: await Promise.all( + config.rules.map(async (rule) => { + if (rule.ruleId === "prh") { + return { + ...rule, + options: await prh(rule.options) + }; + } + return rule; + }) + ) + }; +}; diff --git a/packages/@textlint/config-inliner/src/rules/prh.ts b/packages/@textlint/config-inliner/src/rules/prh.ts new file mode 100644 index 0000000..0af875a --- /dev/null +++ b/packages/@textlint/config-inliner/src/rules/prh.ts @@ -0,0 +1,32 @@ +import fs from "fs/promises"; +import untildify from "untildify"; +import { TextlintRuleOptions } from "@textlint/types"; + +const inlineRuleFiles = (filePaths: string[]): Promise => { + return Promise.all( + filePaths.map((filePath) => { + return fs.readFile(untildify(filePath), "utf-8"); + }) + ); +}; +type PrhOptions = + | { rulePaths: string[] } + | ({ + ruleContents: string[]; + } & TextlintRuleOptions); +/** + * use `ruleContents` instead of `rulePaths` for inlining + * https://github.com/textlint-rule/textlint-rule-prh + */ +export const prh = async ( + options: TextlintRuleOptions | undefined | boolean +): Promise => { + if (typeof options === "boolean" || options === undefined) { + return options; + } + return { + ...options, + rulePaths: [], + ruleContents: await inlineRuleFiles(options.rulePaths) + }; +}; diff --git a/packages/@textlint/config-inliner/test/fixtures/prh/prh.yml b/packages/@textlint/config-inliner/test/fixtures/prh/prh.yml new file mode 100644 index 0000000..e141873 --- /dev/null +++ b/packages/@textlint/config-inliner/test/fixtures/prh/prh.yml @@ -0,0 +1,9 @@ +version: 1 +rules: + - expected: jQuery + specs: + - from: jquery + to: jQuery + - from: JQUERY + to: jQuery + diff --git a/packages/@textlint/config-inliner/test/prh.test.ts b/packages/@textlint/config-inliner/test/prh.test.ts new file mode 100644 index 0000000..52e9491 --- /dev/null +++ b/packages/@textlint/config-inliner/test/prh.test.ts @@ -0,0 +1,16 @@ +import { prh } from "../src/rules/prh"; +import path from "path"; +import assert from "assert"; +import * as fs from "fs"; + +describe("prh", function () { + it("inline rulePaths to ruleContents", async () => { + const prhOptions = await prh({ + rulePaths: [path.join(__dirname, "fixtures/prh/prh.yml")] + }); + assert.deepStrictEqual(prhOptions, { + rulePaths: [], + ruleContents: [fs.readFileSync(path.join(__dirname, "fixtures/prh/prh.yml"), "utf-8")] + }); + }); +}); diff --git a/packages/@textlint/config-inliner/test/tsconfig.json b/packages/@textlint/config-inliner/test/tsconfig.json new file mode 100644 index 0000000..cb359a9 --- /dev/null +++ b/packages/@textlint/config-inliner/test/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "declaration": false, + "noEmit": true + }, + "include": [ + "../src/**/*", + "./**/*" + ] +} \ No newline at end of file diff --git a/packages/@textlint/config-inliner/tsconfig.json b/packages/@textlint/config-inliner/tsconfig.json new file mode 100644 index 0000000..e1d6a02 --- /dev/null +++ b/packages/@textlint/config-inliner/tsconfig.json @@ -0,0 +1,36 @@ +{ + "compilerOptions": { + /* Basic Options */ + "module": "commonjs", + "moduleResolution": "node", + "esModuleInterop": true, + "newLine": "LF", + "outDir": "./lib/", + "target": "es5", + "sourceMap": true, + "declaration": true, + "jsx": "preserve", + "lib": [ + "esnext", + "dom" + ], + /* Strict Type-Checking Options */ + "strict": true, + /* Additional Checks */ + /* Report errors on unused locals. */ + "noUnusedLocals": true, + /* Report errors on unused parameters. */ + "noUnusedParameters": true, + /* Report error when not all code paths in function return a value. */ + "noImplicitReturns": true, + /* Report errors for fallthrough cases in switch statement. */ + "noFallthroughCasesInSwitch": true + }, + "include": [ + "src/**/*" + ], + "exclude": [ + ".git", + "node_modules" + ] +} \ No newline at end of file diff --git a/packages/@textlint/config-inliner/tsconfig.module.json b/packages/@textlint/config-inliner/tsconfig.module.json new file mode 100644 index 0000000..c94d549 --- /dev/null +++ b/packages/@textlint/config-inliner/tsconfig.module.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "ESNext", + "outDir": "./module/", + } +} \ No newline at end of file