Skip to content

Commit

Permalink
feat: basic support for common languages
Browse files Browse the repository at this point in the history
  • Loading branch information
e-cloud committed Oct 28, 2018
1 parent ab13d0e commit be46722
Show file tree
Hide file tree
Showing 39 changed files with 4,221 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Editor configuration, see http://editorconfig.org
root = true

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

[*.md]
max_line_length = off
trim_trailing_whitespace = false

[*.ts]
indent_size = 4
quote_type = single
curly_bracket_next_line = true
indent_brace_style = Allman
spaces_around_operators = true
spaces_around_brackets = inside
continuation_indent_size = 4
26 changes: 26 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Automatically normalize line endings for all text-based files
# http://git-scm.com/docs/gitattributes#_end_of_line_conversion
* text=auto

# For the following file types, normalize line endings to LF on
# checkin and prevent conversion to CRLF when they are checked out
# (this is required in order to prevent newline related issues like,
# for example, after the build script is run)
.* text eol=lf
*.css text eol=lf
*.sass text eol=lf
*.less text eol=lf
*.scss text eol=lf
*.html text eol=lf
*.js text eol=lf
*.ts text eol=lf
*.json text eol=lf
*.md text eol=lf
*.sh text eol=lf
*.txt text eol=lf
*.xml text eol=lf
*.yaml text eol=lf

# Exclude the `.htaccess` file from GitHub's language statistics
# https://github.com/github/linguist#using-gitattributes
dist/.htaccess linguist-vendored
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Node
node_modules
npm-debug.log

# JetBrains
.idea
.project
.settings
*.iml

# VS Code
.vscode

# Windows
Thumbs.db
Desktop.ini

# Mac
.DS_Store
**/.DS_Store

# Build files
dist

yarn-error.log
package-lock.json
35 changes: 35 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Node
node_modules/*
npm-debug.log
docs/*
# DO NOT IGNORE TYPESCRIPT FILES FOR NPM
# TypeScript
# *.js
# *.map
# *.d.ts

# JetBrains
.idea
.project
.settings
.idea/*
*.iml

# VS Code
.vscode/*

# Windows
Thumbs.db
Desktop.ini

# Mac
.DS_Store
**/.DS_Store

# Ngc generated files
**/*.ngfactory.ts

# Library files
src/*
src/ngx-tree/*
build/*
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
dist: trusty
sudo: false

language: node_js
node_js:
- "8"
- "10"

cache:
yarn: true

install: yarn

script: yarn test
Empty file added index.js
Empty file.
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
"testMatch": ["**/test/*.test.js"],
"testEnvironment": "node"
}
24 changes: 24 additions & 0 deletions lib/css-renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const gonzales = require('gonzales-pe');

const { generateBlockCommentContent } = require('./util')

function render(source, template, syntax = 'css') {
const parseTree = gonzales.parse(source, {
syntax
});

const commentNode = gonzales.createNode({ syntax, type: 'multilineComment', content: generateBlockCommentContent(template) })
const blankLineNode = gonzales.createNode({
type: 'space',
content: '\n\n'
})
parseTree.insert(0, blankLineNode)
parseTree.insert(0, commentNode)


return parseTree.toString()
}

module.exports = {
render
}
55 changes: 55 additions & 0 deletions lib/js-renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const recast = require('recast');

const {generateBlockCommentContent} = require('./util')

const builders = recast.types.builders;

function parse(source, parserType = 'babel') {
return recast.parse(source, {
parser: require(`recast/parsers/${parserType}`),
})
}

function print(ast) {
return recast.print(ast, {
lineTerminator: '\n'
}).code
}

function appendCopyright(ast, crText) {
const comment = builders.commentBlock(generateBlockCommentContent(crText));

const firstStatement = ast.program.body[0]

if(firstStatement) {
firstStatement.comments = firstStatement.leadingComments || []

firstStatement.comments.unshift(comment)
}

return ast
}

function detectCopyright(ast) {
const leadingComment = ast.comments[0]

if(leadingComment) {

}
}

function render(source, template, parserType) {
const ast = parse(source, parserType)

const alteredAst = appendCopyright(ast, template)

return addBlankLineBetweenCopyrightAndCode(print(alteredAst));
}

function addBlankLineBetweenCopyrightAndCode(fileString) {
return fileString.replace(/\*\/$/m, '*/\n');
}

module.exports = {
render
};
7 changes: 7 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function generateBlockCommentContent(text, lineChar = '*', needTopChar = true) {
return `${needTopChar ? lineChar : ''}\n${text.trim().split('\n').map(line => (` ${lineChar} ${line}`).trimRight()).join('\n')}\n `
}

module.exports = {
generateBlockCommentContent
}
23 changes: 23 additions & 0 deletions lib/xml-renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const xmlCommentRegexp = /$(<!--([\s\S]*?)-->)/g;

const xmlCommentContentRegexp = /<!--([\s\S]*?)-->/g;

const { generateBlockCommentContent } = require('./util');

function render(source, template) {
const comment = `<!--${generateBlockCommentContent(template, '-', false)}-->\n\n`

const matches = xmlCommentRegexp.exec(source.trim())

let result

if (!matches) {
result = comment + source
}

return result
}

module.exports = {
render
}
22 changes: 22 additions & 0 deletions lib/yaml-renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const yaml = require('yaml');

function render(source, template) {
const templateLines = template.split('\n')
const comment = templateLines.map(line => (' ' + line).trimRight()).join('\n')

const doc = yaml.parseDocument(source, {
keepCstNodes: true
})

doc.commentBefore = comment

const preout = doc.toString()

const lastTemplateLine = templateLines[templateLines.length - 1]

return preout.replace(lastTemplateLine, lastTemplateLine + '\n')
}

module.exports = {
render
}
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "copyrightizen",
"version": "1.0.0",
"description": "A tool generates designated copyright notice to target files with scope support like Intellij IDEs.",
"main": "index.js",
"scripts": {
"test": "mocha",
"jest": "jest"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/e-cloud/copyrightizen.git"
},
"keywords": [
"copyright",
"generate",
"license"
],
"author": "e-cloud <saintscott119@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/e-cloud/copyrightizen/issues"
},
"homepage": "https://github.com/e-cloud/copyrightizen#readme",
"dependencies": {
"@babel/parser": "^7.1.3",
"@babel/traverse": "^7.1.4",
"@babel/types": "^7.1.3",
"fs-extra": "^7.0.0",
"glob": "^7.1.3",
"gonzales-pe": "^4.2.3",
"recast": "^0.16.0",
"yaml": "^1.0.0"
},
"devDependencies": {
"chai": "^4.2.0",
"chai-files": "^1.4.0",
"jest": "^23.6.0",
"mocha": "^5.2.0"
}
}
17 changes: 17 additions & 0 deletions test/__baselines__/css/basic.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (C) xxx Systems, Inc - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by John Doe <jdoe@heaven.com>, 1984
*
* From https://softwareengineering.stackexchange.com/a/68150/266765
*/

body {
background-color: lightblue;
}

h1 {
color: navy;
margin-left: 20px;
}
24 changes: 24 additions & 0 deletions test/__baselines__/html/basic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
- Copyright (C) xxx Systems, Inc - All Rights Reserved
- Unauthorized copying of this file, via any medium is strictly prohibited
- Proprietary and confidential
- Written by John Doe <jdoe@heaven.com>, 1984
-
- From https://softwareengineering.stackexchange.com/a/68150/266765
-->

<!DOCTYPE html>
<html>

<head>
<title>Page Title</title>
</head>

<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>

</html>
14 changes: 14 additions & 0 deletions test/__baselines__/js/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (C) xxx Systems, Inc - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by John Doe <jdoe@heaven.com>, 1984
*
* From https://softwareengineering.stackexchange.com/a/68150/266765
*/

function add(a, b) {
return a + b;
}

module.exports = add;

0 comments on commit be46722

Please sign in to comment.