Pārlūkot izejas kodu

chore: Set up changelog generation script

Michael Bromley 6 gadi atpakaļ
vecāks
revīzija
052cb61072

+ 24 - 9
CONTRIBUTING.md

@@ -1,15 +1,9 @@
 # Contribution Guidelines
 
-## Note: code contributions discouraged in pre-alpha phase
-
-Due to the very early phase of this project, significant changes to code and architecture are still taking place.
-
-Therefore I do not recommend investing time in making code changes until things are more stable.
+### Commit message format
 
-Contributions in terms of suggestions or other relevant feedback are welcome.
+This repo uses [Conventional Commits](https://www.conventionalcommits.org).
 
-### Commit message format
-Ref: [Karma commit message conventions](http://karma-runner.github.io/0.10/dev/git-commit-msg.html)
 ```
 type(scope): Message in present tense
 ```
@@ -22,4 +16,25 @@ type(scope): Message in present tense
 * **test** (adding missing tests, refactoring tests; no production code change)
 * **chore** (updating build tasks etc; no production code change)
 
-`scope` indicates the package (core, asset-server-plugin, docs etc.)
+`scope` indicates the package affected by the commit:
+
+* admin-ui
+* admin-ui-plugin
+* asset-server-plugin
+* common
+* core
+* create
+* email-plugin
+* etc.
+
+If a commit affects more than one package, seperate them with a comma:
+
+```
+fix(core,common): Fix the thing
+```
+
+If a commit applies to no particular package (e.g. a tooling change in the root package.json), the scope can be omitted.
+
+#### Linting
+
+Commit messages are linted on commit, so you'll know if your message is not quite right.

+ 23 - 4
package.json

@@ -1,6 +1,6 @@
 {
   "name": "vendure",
-  "version": "0.1.0-alpha.17",
+  "version": "0.0.0",
   "private": true,
   "scripts": {
     "core:watch": "concurrently -n tsc,gulp,common \"cd packages/core && yarn tsc:watch\" \"cd packages/core && yarn gulp:watch\" \"cd packages/common && yarn watch\"",
@@ -16,6 +16,7 @@
     "lint:admin-ui": "cd admin-ui && yarn lint --fix",
     "precommit": "lint-staged",
     "postcommit": "git update-index --again",
+    "commitmsg": "commitlint -e $GIT_PARAMS",
     "prepush": "yarn test:all && cd admin-ui && yarn build --prod",
     "dev-server:start": "cd packages/dev-server && yarn start",
     "dev-server:populate": "cd packages/dev-server && yarn populate",
@@ -26,9 +27,12 @@
     "test:e2e": "jest --config packages/core/e2e/config/jest-e2e.json --runInBand",
     "test:admin-ui": "cd admin-ui && yarn test --watch=false --browsers=ChromeHeadlessCI --progress=false",
     "build": "lerna run build",
+    "generate-changelogs": "ts-node scripts/changelogs/changelogs.ts",
     "publish:packages": "yarn build && lerna publish --exact -m \"chore: Publish %s release\" --no-git-tag-version"
   },
   "devDependencies": {
+    "@commitlint/cli": "^7.6.1",
+    "@commitlint/config-conventional": "^7.6.0",
     "@graphql-codegen/add": "^1.1.3",
     "@graphql-codegen/cli": "^1.1.3",
     "@graphql-codegen/typescript": "^1.1.3",
@@ -38,12 +42,13 @@
     "@types/klaw-sync": "^6.0.0",
     "@types/node": "^10.11.5",
     "concurrently": "^4.1.0",
+    "conventional-changelog-core": "^3.2.2",
     "graphql": "^14.1.1",
     "graphql-tools": "^4.0.0",
-    "husky": "^0.14.3",
+    "husky": "^2.3.0",
     "jest": "^24.5.0",
     "klaw-sync": "^6.0.0",
-    "lerna": "^3.13.1",
+    "lerna": "^3.14.1",
     "lint-staged": "^7.2.0",
     "prettier": "^1.15.2",
     "ts-jest": "^24.0.0",
@@ -53,5 +58,19 @@
   },
   "workspaces": [
     "packages/*"
-  ]
+  ],
+  "commitlint": {
+    "extends": [
+      "@commitlint/config-conventional"
+    ],
+    "rules": {
+      "subject-case": [
+        2,
+        "always",
+        [
+          "sentence-case"
+        ]
+      ]
+    }
+  }
 }

+ 42 - 0
scripts/changelogs/add-stream.ts

@@ -0,0 +1,42 @@
+import * as Stream from 'stream';
+import { PassThrough, Writable } from 'stream';
+
+class Appendee extends PassThrough {
+    constructor(private factory: any, private opts: any) {
+        super(opts);
+    }
+
+    _flush(end: any) {
+        const stream = this.factory();
+        stream.pipe(new Appender(this, this.opts))
+            .on('finish', end);
+        stream.resume();
+    }
+}
+
+class Appender extends Writable {
+    constructor(private target: any, opts: any) {
+        super(opts);
+    }
+
+    _write(chunk: any, enc: any, cb: any) {
+        this.target.push(chunk);
+        cb();
+    }
+}
+
+/**
+ * Append the contents of one stream onto another.
+ * Based on https://github.com/wilsonjackson/add-stream
+ */
+export function addStream(stream: any, opts?: any) {
+    opts = opts || {};
+    let factory;
+    if (typeof stream === 'function') {
+        factory = stream;
+    } else {
+        stream.pause();
+        factory = () => stream;
+    }
+    return new Appendee(factory, opts);
+}

+ 98 - 0
scripts/changelogs/changelogs.ts

@@ -0,0 +1,98 @@
+import fs from 'fs-extra';
+import path from 'path';
+
+import { addStream } from './add-stream';
+// tslint:disable-next-line:no-var-requires
+const conventionalChangelogCore = require('conventional-changelog-core');
+
+type PackageDef = { name: string | string[]; path: string; };
+
+/**
+ * The types of commit which will be included in the changelog.
+ */
+const TYPES_TO_INCLUDE = ['feat', 'fix'];
+
+/**
+ * Define which packages to create changelog entries for. The `name` property should correspond to the
+ * "scope" as used in the commit message.
+ */
+const PACKAGES: PackageDef[] = [
+    { name: ['admin-ui-plugin', 'admin-ui'], path: path.join(__dirname, '../../packages/admin-ui-plugin') },
+    { name: ['asset-server', 'asset-server-plugin'], path: path.join(__dirname, '../../packages/asset-server-plugin') },
+    { name: 'common', path: path.join(__dirname, '../../packages/common') },
+    { name: 'core', path: path.join(__dirname, '../../packages/core') },
+    { name: 'create', path: path.join(__dirname, '../../packages/create') },
+    { name: ['email-plugin', 'email'], path: path.join(__dirname, '../../packages/email-plugin') },
+];
+
+const mainTemplate = fs.readFileSync(path.join(__dirname, 'template.hbs'), 'utf-8');
+const commitTemplate = fs.readFileSync(path.join(__dirname, 'commit.hbs'), 'utf-8');
+
+PACKAGES.forEach(generateChangelogForPackage);
+
+/**
+ * Generates changelog entries for the given package based on the conventional commits data.
+ */
+function generateChangelogForPackage(pkg: PackageDef) {
+    const changelogPath = path.join(pkg.path, 'CHANGELOG.md');
+    const inStream = fs.createReadStream(changelogPath, { flags: 'a+' });
+    const tempFile = path.join(__dirname, `__temp_${pkg.name}__`);
+    conventionalChangelogCore({
+            pkg: {
+                path: path.join(pkg.path, 'package.json'),
+            },
+            transform: (commit: any, context: any) => {
+                const includeCommit = scopeMatchesName(commit.scope, pkg.name) && TYPES_TO_INCLUDE.includes(commit.type);
+                if (includeCommit) {
+                    return context(null, commit);
+                } else {
+                    return context(null, null);
+                }
+
+            },
+            releaseCount: 1,
+            outputUnreleased: false,
+        },
+        null,
+        null,
+        null,
+        {
+            mainTemplate,
+            commitPartial: commitTemplate,
+            finalizeContext(context: any, options: any, commits: any) {
+                context.commitGroups.forEach(addHeaderToCommitGroup);
+                return context;
+            },
+        })
+        .pipe(addStream(inStream))
+        .pipe(fs.createWriteStream(tempFile))
+        .on('finish', () => {
+            fs.createReadStream(tempFile)
+                .pipe(fs.createWriteStream(changelogPath))
+                .on('finish', () => {
+                    fs.unlinkSync(tempFile);
+                });
+        });
+}
+
+function scopeMatchesName(scope: string, name: string | string[]): boolean {
+    return Array.isArray(name) ? name.includes(scope) : scope === name;
+}
+
+/**
+ * The `header` is a more human-readable version of the commit type, as used in the
+ * template.hbs as a sub-heading.
+ */
+function addHeaderToCommitGroup(commitGroup: any) {
+    switch (commitGroup.title) {
+        case 'fix':
+            commitGroup.header = 'Fixes';
+            break;
+        case 'feat':
+            commitGroup.header = 'Features';
+            break;
+        default:
+            commitGroup.header = commitGroup.title.charAt(0).toUpperCase() + commitGroup.title.slice(1);
+            break;
+    }
+}

+ 56 - 0
scripts/changelogs/commit.hbs

@@ -0,0 +1,56 @@
+* {{subject}}
+
+{{~!-- commit link --}} {{#if @root.linkReferences~}}
+  ([{{hash}}](
+  {{~#if @root.repository}}
+    {{~#if @root.host}}
+      {{~@root.host}}/
+    {{~/if}}
+    {{~#if @root.owner}}
+      {{~@root.owner}}/
+    {{~/if}}
+    {{~@root.repository}}
+  {{~else}}
+    {{~@root.repoUrl}}
+  {{~/if}}/
+  {{~@root.commit}}/{{hash}}))
+{{~else}}
+  {{~hash}}
+{{~/if}}
+
+{{~!-- commit references --}}
+{{~#if references~}}
+  , closes
+  {{~#each references}} {{#if @root.linkReferences~}}
+    [
+    {{~#if this.owner}}
+      {{~this.owner}}/
+    {{~/if}}
+    {{~this.repository}}#{{this.issue}}](
+    {{~#if @root.repository}}
+      {{~#if @root.host}}
+        {{~@root.host}}/
+      {{~/if}}
+      {{~#if this.repository}}
+        {{~#if this.owner}}
+          {{~this.owner}}/
+        {{~/if}}
+        {{~this.repository}}
+      {{~else}}
+        {{~#if @root.owner}}
+          {{~@root.owner}}/
+        {{~/if}}
+          {{~@root.repository}}
+        {{~/if}}
+    {{~else}}
+      {{~@root.repoUrl}}
+    {{~/if}}/
+    {{~@root.issue}}/{{this.issue}})
+  {{~else}}
+    {{~#if this.owner}}
+      {{~this.owner}}/
+    {{~/if}}
+    {{~this.repository}}#{{this.issue}}
+  {{~/if}}{{/each}}
+{{~/if}}
+

+ 12 - 0
scripts/changelogs/template.hbs

@@ -0,0 +1,12 @@
+{{> header}}
+
+{{#each commitGroups}}
+
+#### {{ this.header }}
+
+{{#each commits}}
+{{> commit root=@root}}
+{{/each}}
+{{/each}}
+
+{{> footer}}

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 471 - 234
yarn.lock


Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels