Skip to content

Commit 4349931

Browse files
scaffold codemod
1 parent 56d8262 commit 4349931

File tree

16 files changed

+224
-0
lines changed

16 files changed

+224
-0
lines changed

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# DEP0081: `fs.truncate()` using a file descriptor
2+
3+
This recipe transforms the usage of `fs.truncate()` to `fs.ftruncateSync()` when a file descriptor is used.
4+
5+
See [DEP0081](https://nodejs.org/api/deprecations.html#DEP0081).
6+
7+
## Example
8+
9+
**Before:**
10+
```js
11+
const { truncate, open, close } = require('node:fs');
12+
13+
open('file.txt', 'w', (err, fd) => {
14+
if (err) throw err;
15+
truncate(fd, 10, (err) => {
16+
if (err) throw err;
17+
close(fd, () => {});
18+
});
19+
});
20+
```
21+
22+
**After:**
23+
```js
24+
const { ftruncate, open, close } = require('node:fs');
25+
26+
open('file.txt', 'w', (err, fd) => {
27+
if (err) throw err;
28+
ftruncate(fd, 10, (err) => {
29+
if (err) throw err;
30+
close(fd, () => {});
31+
});
32+
});
33+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
schema_version: "1.0"
2+
name: nodejs/create-require-from-path
3+
version: 0.0.1
4+
description: Handle DEP0081 via transforming `truncate` to `ftruncateSync` when using a file descriptor.
5+
author: Augustin Mauroy
6+
license: MIT
7+
workflow: workflow.yaml
8+
category: migration
9+
10+
targets:
11+
languages:
12+
- javascript
13+
- typescript
14+
15+
keywords:
16+
- transformation
17+
- migration
18+
19+
registry:
20+
access: public
21+
visibility: public
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "@nodejs/fs-truncate-fs-depreciation",
3+
"version": "0.0.1",
4+
"description": "Handle DEP0081 via transforming `truncate` to `ftruncateSync` when using a file descriptor.",
5+
"type": "module",
6+
"scripts": {
7+
"test": "npx codemod@next jssg test -l typescript ./src/workflow.ts ./"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/nodejs/userland-migrations.git",
12+
"directory": "recipes/fs-truncate-fs-depreciation",
13+
"bugs": "https://github.com/nodejs/userland-migrations/issues"
14+
},
15+
"author": "Augustin Mauroy",
16+
"license": "MIT",
17+
"homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/fs-truncate-fs-depreciation/README.md",
18+
"devDependencies": {
19+
"@codemod.com/jssg-types": "^1.0.3"
20+
},
21+
"dependencies": {
22+
"@nodejs/codemod-utils": "0.0.0"
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { SgRoot, Edit } from "@codemod.com/jssg-types/main";
2+
3+
/**
4+
*
5+
*/
6+
export default function transform(root: SgRoot): string | null {
7+
const rootNode = root.root();
8+
const edits: Edit[] = [];
9+
let hasChanges = false;
10+
11+
12+
if (!hasChanges) return null;
13+
14+
return rootNode.commitEdits(edits);
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { ftruncate, open, close } = require('node:fs');
2+
3+
open('file.txt', 'w', (err, fd) => {
4+
if (err) throw err;
5+
ftruncate(fd, 10, (err) => {
6+
if (err) throw err;
7+
close(fd, () => { });
8+
});
9+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const fs = require('node:fs');
2+
3+
const fd = fs.openSync('file.txt', 'w');
4+
try {
5+
fs.ftruncateSync(fd, 10);
6+
} finally {
7+
fs.closeSync(fd);
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { truncate, ftruncateSync, open, openSync, close, closeSync } = require('node:fs');
2+
3+
// This should be replaced (file descriptor)
4+
const fd = openSync('file.txt', 'w');
5+
ftruncateSync(fd, 10);
6+
closeSync(fd);
7+
8+
// This should NOT be replaced (file path)
9+
truncate('other.txt', 5, (err) => {
10+
if (err) throw err;
11+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ftruncate, open, close } from 'node:fs';
2+
3+
open('file.txt', 'w', (err, fd) => {
4+
if (err) throw err;
5+
ftruncate(fd, 10, (err) => {
6+
if (err) throw err;
7+
close(fd, () => { });
8+
});
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import fs from 'node:fs';
2+
3+
fs.open('file.txt', 'w', (err, fd) => {
4+
if (err) throw err;
5+
fs.ftruncate(fd, 10, (err) => {
6+
if (err) throw err;
7+
fs.close(fd, () => { });
8+
});
9+
});

0 commit comments

Comments
 (0)