# 开发者指南
要想编辑或改造 Cornerstone,您将需要安装 Node.js (opens new window)。npm 包管理器随着 Node.js 附带一起安装,你可以使用它们来安装项目依赖,并且运行常用的开发任务。
# 常用任务
# 安装依赖
npm install
注意
安装/更新依赖项应该在git存储库的每次更新之后执行。如果不执行此操作,您可能会在开发过程中遇到问题。
# 运行生成
npm start
运行生成将同时创建这个库的压缩和未压缩的版本,以及它们各自的 source map 文件。
# 在每次变更后自动运行生成
npm run watch
如果要调试问题或向源代码添加新功能,可以使用此命令。
# 发布开发使用的文件服务
有很多方法运行一个简单的 HTTP 服务器。下面只举一个例,您也可以使用其它,如 python -m simplehttpserver 8080
- 安装
http-server
包:
npm install http-server -g
提示
有时候您可能需要使用 sudo 去执行全局安装。
- 运行服务器
http-server
注意
要想在移动设备上访问这些,您需要运行 HTTP 服务器,然后通过电脑 IP 地址进行访问(比如:http://192.168.1.11:8080/example/index.html (opens new window))
# 运行和调试测试用例
npm test
运行测试时,将创建一个“coverage”目录。请注意,因为它只是一个构建生成文件,所以在主代码库中不存在这个目录。通过浏览器打开“coverage/html/index.html”这个 HTML 文件(不必启用一个 HTTP 服务器),您能够查看和检查测试覆盖报告。
当启动了测试之后,您还可以用浏览器访问 http://0.0.0.0:9876/debug ,通过 Karma 测试程序去进行测试的调试。请注意,这个 URL 不会立即生效,因为 npm test
命令的第一步是重新构建整个库。
# 运行代码风格检测
npm run eslint
# Or include automatic fixing with:
npm run eslint-fix
# Or automatically fix 'test' directory with:
npm run eslint-fix-test
Running the commands above will check the source code for linting issues.
# Submitting changes through Pull Requests
If you have made a source code change that you think should be included in the main repository, you can contribute it back to the community by creating a Pull Request (opens new window). Please create an associated Issue (opens new window) to describe the problem you are solving / feature you are adding so the library maintainers can give you feedback on whether or not these changes are appropriate for the repository. It's possible that your bug fix / new feature would be better implemented in another library (e.g. Cornerstone Tools (opens new window)). Please ensure that all tests pass and you have run ESLint and fixed any issues before submitting a pull request.
# Development Toolchain and Specifications
Cornerstone relies on a number of open source tools, components, and free services to ensure quality and stability. We want to ensure developers can work in modern JavaScript and that end users can use the tools in both current and legacy environments.
# General
- Babel (opens new window) for transpilation from next generation JavaScript to more widely supported previous JavaScript versions.
- WebPack (opens new window) to bundle the project
- ESLint (opens new window) is used to enforce code styling and maintain quality
- NPM (opens new window) is used to host the installable package. See Cornerstone Core on NPM (opens new window)
- Semantic Versioning (opens new window) is used for versioning of the library.
- keep a changelog (opens new window) is used for the formatting of the changelog.
# Testing
- Karma (opens new window) is used as a test runner
- Mocha (opens new window) is used as a test framework
- Chai (opens new window) is used for test assertions
- Istanbul (opens new window) is used to report code coverage
- Travis CI (opens new window) is used to automatically run tests. See Cornerstone Core on Travis CI (opens new window)
- Coveralls (opens new window) is used to display code coverage following automatic tests. See Cornerstone Core Coveralls Test Coverage (opens new window)
- Headless Chrome (opens new window) is used for running headless tests.
# Documentation
- JSDoc (opens new window) formatting is used for documenting the source code.
- documentation.js (opens new window) generates API documentation in Markdown from JSDoc annotations
- GitBook (opens new window) transforms the Markdown documentation into HTML
- Github Pages (opens new window) hosts the documentation
- Cloudflare (opens new window) is placed in front of Github Pages to serve the documentation over HTTPS.
- Rawgit (opens new window) is used to serve Live Examples from the repository. We should transition this to be hosted with Github Pages as well.
# Writing tests
Here are some general notes on writing tests which may be useful:
- Tests must be in the 'test' directory. Please try to ensure they follow the 'src' directory layout for simplicity.
- Test file names must end in "_test.js"
- The 'coverage_test.js' file ensures that all files are considered by Istanbul, so that code coverage reports can be used to explore the whole repository.
- Do not convert 'function ()' to arrow functions (i.e. '=> {}') within the 'it', 'should', or 'describe' blocks or Mocha will fail to run the tests properly.
# Releasing a new version
- Make sure you have the latest commits from master
- Determine the version change you need to make based on the scope of the changes since the last release.
- Update the changelog (opens new window)
- Make sure to thank any code contributors!
- Update the package and dependency versions in "package.json"
- Update the build version:
npm run version
- Run the build:
npm run build
- Commit the changes
git commit -am "Bump version <version>"
- Tag the commits with the version number and title
git tag -a "<version>" -m "Version <version>"
- Push the commit with the version number to master
git push origin master --tags
- Publish the release to NPM:
npm publish
# Updating Docs with gitbook
# If its your first time:
- Make sure you have gitbook-cli installed globally, if not, run:
npm install -g gitbook-cli
- Go inside the /docs folder and do:
gitbook install
# Updating files and deploying
- On the root folder run:
npm run docs
# It will serve the documentation on http://localhost:4000/
# with livereload
- Change the docs needed and make sure everything is correct
- In order to deploy, run:
npm run docs:deploy