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

Switch to JSR deployement #14

Merged
merged 12 commits into from
May 17, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/deno-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Setup Deno
uses: denoland/[email protected]
with:
deno-version: v1.40.x
deno-version: v1.43.3

- name: Node setup
uses: actions/[email protected]
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/deno-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ jobs:
uses: denoland/[email protected]
# uses: denoland/setup-deno@9db7f66e8e16b5699a514448ce994936c63f0d54
with:
deno-version: v1.40.x # required due to usage of Deno.stdout.isTerminal()
deno-version: v1.43.3
# deno-version: v1.40.x # required due to usage of Deno.stdout.isTerminal()
# deno-version: v1.x

- name: Verify formatting
Expand Down
47 changes: 47 additions & 0 deletions .github/workflows/jsr-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Publish to JSR

on:
release:
types: [created]
workflow_dispatch:
inputs:
npm_version:
description: 'Version number to publish on JSR X.Y.Z'
required: true
permissions:
contents: read

jobs:
publish:
runs-on: ubuntu-latest

permissions:
contents: read
id-token: write

steps:
- name: Setup repo
uses: actions/[email protected] # use nodeJS 20

- name: Setup Deno
uses: denoland/[email protected]
with:
deno-version: v1.43.3

- name: Node setup
uses: actions/[email protected]
with:
node-version: 20
registry-url: https://registry.npmjs.org/

- name: Run linter
run: deno lint

- name: Run tests
run: deno test -A

- name: show deno version
run: deno --version

- name: Publish package ${{inputs.npm_version}}
run: npx jsr publish
58 changes: 29 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# deno-logger

[![NPM Version](https://img.shields.io/npm/v/@denodnt/logger.svg?style=flat)](https://www.npmjs.org/package/@denodnt/logger)
[![JSR Version](https://jsr.io/badges/@deno-lib/logger)](https://jsr.io/@deno-lib/logger)

Deno / NodeJS colorful logger colorful logger

For Deno usage refer to [deno-logger doc](https://deno.land/x/logger)
Expand All @@ -10,7 +12,7 @@ For Deno usage refer to [deno-logger doc](https://deno.land/x/logger)
### console logger

```js
import Logger from "https://deno.land/x/[email protected].5/logger.ts";
import Logger from "https://deno.land/x/[email protected].6/logger.ts";

const logger = new Logger();

Expand All @@ -22,7 +24,7 @@ logger.error("i am from consoleLogger", new Error("test"));
### file and console logger

```js
import Logger from "https://deno.land/x/[email protected].5/logger.ts";
import Logger from "https://deno.land/x/[email protected].6/logger.ts";

const logger = new Logger();

Expand All @@ -42,7 +44,7 @@ logger.error("i am from fileLogger", new Error("test"));
### file logger only

```js
import Logger from "https://deno.land/x/[email protected].5/logger.ts";
import Logger from "https://deno.land/x/[email protected].6/logger.ts";

const logger = new Logger();
await logger.initFileLogger("../log");
Expand All @@ -68,7 +70,7 @@ interface fileLoggerOptions {
example

```js
import Logger from "https://deno.land/x/[email protected].5/logger.ts";
import Logger from "https://deno.land/x/[email protected].6/logger.ts";
const logger = new Logger();

// cut by day
Expand Down Expand Up @@ -97,25 +99,23 @@ await logger.initFileLogger("../log", {
// filename is [type].log.[n]
// example info.log.1, info.log.2 ...
// when reach maxBackupCount, the [type].log.[maxBackupCount-1] will be overwrite
/*
detail:
`maxBytes` specifies the maximum size
in bytes that the log file can grow to before rolling over to a new one. If the
size of the new log message plus the current log file size exceeds `maxBytes`
then a roll over is triggered. When a roll over occurs, before the log message
is written, the log file is renamed and appended with `.1`. If a `.1` version
already existed, it would have been renamed `.2` first and so on. The maximum
number of log files to keep is specified by `maxBackupCount`. After the renames
are complete the log message is written to the original, now blank, file.

Example: Given `log.txt`, `log.txt.1`, `log.txt.2` and `log.txt.3`, a
`maxBackupCount` of 3 and a new log message which would cause `log.txt` to
exceed `maxBytes`, then `log.txt.2` would be renamed to `log.txt.3` (thereby
discarding the original contents of `log.txt.3` since 3 is the maximum number of
backups to keep), `log.txt.1` would be renamed to `log.txt.2`, `log.txt` would
be renamed to `log.txt.1` and finally `log.txt` would be created from scratch
where the new log message would be written.
*/
// detail:
// `maxBytes` specifies the maximum size
// in bytes that the log file can grow to before rolling over to a new one. If the
// size of the new log message plus the current log file size exceeds `maxBytes`
// then a roll over is triggered. When a roll over occurs, before the log message
// is written, the log file is renamed and appended with `.1`. If a `.1` version
// already existed, it would have been renamed `.2` first and so on. The maximum
// number of log files to keep is specified by `maxBackupCount`. After the renames
// are complete the log message is written to the original, now blank, file.
//
// Example: Given `log.txt`, `log.txt.1`, `log.txt.2` and `log.txt.3`, a
// `maxBackupCount` of 3 and a new log message which would cause `log.txt` to
// exceed `maxBytes`, then `log.txt.2` would be renamed to `log.txt.3` (thereby
// discarding the original contents of `log.txt.3` since 3 is the maximum number of
// backups to keep), `log.txt.1` would be renamed to `log.txt.2`, `log.txt` would
// be renamed to `log.txt.1` and finally `log.txt` would be created from scratch
// where the new log message would be written.
await logger.initFileLogger("../log", {
maxBytes: 10 * 1024,
maxBackupCount: 10,
Expand Down Expand Up @@ -158,7 +158,7 @@ await logger.initFileLogger("../log", {
## disableConsole and enableConsole

```js
import Logger from "https://deno.land/x/[email protected].5/logger.ts";
import Logger from "https://deno.land/x/[email protected].6/logger.ts";

const logger = new Logger();

Expand All @@ -175,7 +175,7 @@ logger.info("console enabled, you can see me");
## disableFile and enableFile

```js
import Logger from "https://deno.land/x/[email protected].5/logger.ts";
import Logger from "https://deno.land/x/[email protected].6/logger.ts";

const logger = new Logger();
await logger.initFileLogger("../log");
Expand All @@ -202,7 +202,7 @@ example:
3. fileLogger, consoleLogger => disable => enable => fileLogger, consoleLogger

```js
import Logger from "https://deno.land/x/[email protected].5/logger.ts";
import Logger from "https://deno.land/x/[email protected].6/logger.ts";

const logger = new Logger();
await logger.initFileLogger("../log");
Expand All @@ -228,7 +228,7 @@ To convert your Deno project to a dual-stack npm project, you should use
`scripts/build_npm.ts` that looks like:

```ts
import { build, emptyDir } from "https://deno.land/x/dnt/mod.ts";
import { build, emptyDir } from "@deno/dnt";

// grap the next version number as you want
const version: Deno.args[0];
Expand All @@ -249,9 +249,9 @@ await build({
},
// map your favorite deno logger to its npm port.
mappings: {
"https://deno.land/x/logger@v1.2.0/logger.ts": {
"https://deno.land/x/logger@v1.1.6/logger.ts": {
name: "@denodnt/logger",
version: "1.2.0",
version: "1.1.6",
peerDependency: false,
},
},
Expand Down
9 changes: 3 additions & 6 deletions _build_npm.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// dnt deps can not be moved to dev_deps.ts
import {
build,
emptyDir,
type PackageJson,
} from "https://deno.land/x/[email protected]/mod.ts";
import * as pc from "https://deno.land/[email protected]/fmt/colors.ts";
import { build, emptyDir } from "@deno/dnt";
import type { PackageJson } from "@deno/dnt";
import * as pc from "jsr:@std/[email protected]/colors";

export async function buildDnt() {
let version = Deno.args[0];
Expand Down
40 changes: 40 additions & 0 deletions _updateMod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const readmeContentOriginal = Deno.readTextFileSync("README.md");
const jsonStr = Deno.readTextFileSync("deno.jsonc");
const jsonConf = JSON.parse(jsonStr) as { version: string };

let readmeContent = readmeContentOriginal.replaceAll(
/https:\/\/deno.land\/x\/logger@v[\d.]+\/logger.ts/g,
`https://deno.land/x/logger@v${jsonConf.version}/logger.ts`,
).replaceAll(
/version: "[\d.]+"/g,
`version: "${jsonConf.version}"`,
);

// update current version in readme
if (readmeContent !== readmeContentOriginal) {
Deno.writeTextFileSync("README.md", readmeContent);
}

readmeContent = readmeContent.replaceAll(
/https:\/\/deno.land\/x\/logger@v[\d.]+\/logger.ts/g,
`jsr:@deno-lib/logger@${jsonConf.version}/logger`,
);

// Write the updated content back to the mod.ts file
Deno.writeTextFileSync(
"mod.ts",
`/**
* ${readmeContent}
* @module
*/

/**
* The Logger class
*/
export { default as Logger } from "./logger.ts";
/**
* The Logger class default instance
*/
export { default as default } from "./logger.ts";
`,
);
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Changelog

### v1.1.6 - 2024.05.17

- use JSR publish
- add docs

### v1.1.1 - 2023.05.19

- New build / deployement script
Expand Down
17 changes: 17 additions & 0 deletions date.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
// const format = n => n > 9 ? n : `0${n}`;
const format = (n: number) => n.toString().padStart(2, "0");

/**
* Dater extends Date to provide more formating methods.
*/
export default class Dater extends Date {
/**
* Get date string as yyyy-mm-dd hh:mm:ss
* @param separator
* @returns
*/
toLocaleString(separator = "-"): string {
const dateString = this.toLocaleDateString(separator);
const timeString = this.toLocaleTimeString();
return `${dateString} ${timeString}`;
}

/**
* Get date string as yyyy-mm-dd
* @param separator
* @returns
*/
toLocaleDateString(separator = "-"): string {
const year = format(this.getFullYear());
const month = format(this.getMonth() + 1);
const day = format(this.getDate());
return `${year}${separator}${month}${separator}${day}`;
}

/**
* Get time string as hh:mm:ss
* @returns
*/
toLocaleTimeString(): string {
const hour = format(this.getHours());
const minute = format(this.getMinutes());
Expand Down
13 changes: 13 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"name": "@deno-lib/logger",
"version": "1.1.6",
"exports": "./mod.ts",
"tasks": {
"regen": "deno run -A _updateMod.ts"
},
"fmt": {
"exclude": ["npm/"]
},
Expand All @@ -7,5 +13,12 @@
},
"test": {
"exclude": ["npm/"]
},
"publish": {
"include": ["*.ts", "README.md", "LICENSE", "changelog.md", "deno.jsonc"],
"exclude": ["_*.ts"]
},
"imports": {
"@deno/dnt": "jsr:@deno/dnt@^0.41.1"
}
}
Loading