Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler): support to inline prh.yml #47

Merged
merged 1 commit into from
May 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions packages/@textlint/config-inliner/.gitignore
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions packages/@textlint/config-inliner/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": [
"ts-node-test-register"
]
}
19 changes: 19 additions & 0 deletions packages/@textlint/config-inliner/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
61 changes: 61 additions & 0 deletions packages/@textlint/config-inliner/README.md
Original file line number Diff line number Diff line change
@@ -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
63 changes: 63 additions & 0 deletions packages/@textlint/config-inliner/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
19 changes: 19 additions & 0 deletions packages/@textlint/config-inliner/src/config-inliner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { TextlintConfigDescriptor } from "@textlint/config-loader";
import { prh } from "./rules/prh";

export const inlineConfig = async (config: TextlintConfigDescriptor): Promise<TextlintConfigDescriptor> => {
return {
...config,
rules: await Promise.all(
config.rules.map(async (rule) => {
if (rule.ruleId === "prh") {
return {
...rule,
options: await prh(rule.options)
};
}
return rule;
})
)
};
};
32 changes: 32 additions & 0 deletions packages/@textlint/config-inliner/src/rules/prh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from "fs/promises";
import untildify from "untildify";
import { TextlintRuleOptions } from "@textlint/types";

const inlineRuleFiles = (filePaths: string[]): Promise<string[]> => {
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<PrhOptions | undefined | boolean> => {
if (typeof options === "boolean" || options === undefined) {
return options;
}
return {
...options,
rulePaths: [],
ruleContents: await inlineRuleFiles(options.rulePaths)
};
};
9 changes: 9 additions & 0 deletions packages/@textlint/config-inliner/test/fixtures/prh/prh.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 1
rules:
- expected: jQuery
specs:
- from: jquery
to: jQuery
- from: JQUERY
to: jQuery

16 changes: 16 additions & 0 deletions packages/@textlint/config-inliner/test/prh.test.ts
Original file line number Diff line number Diff line change
@@ -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")]
});
});
});
Loading