New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

intl-msgs-defaults

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

intl-msgs-defaults - npm Package Compare versions

Comparing version

to
0.0.2

21

bin/apply.js

@@ -45,4 +45,5 @@ "use strict";

sourceType: "module",
plugins: ["jsx"]
plugins: ["jsx", "classProperties"]
});
let writeChanges = false;
(0, _traverse.default)(ast, {

@@ -63,2 +64,3 @@ JSXOpeningElement: path => {

attributePath.insertAfter(t.jsxAttribute(t.jsxIdentifier("defaultMessage"), t.stringLiteral(defaultMessage)));
writeChanges = true;
}

@@ -74,3 +76,3 @@ }

})) {
(0, _helpers.insertDefaultMessageProperty)({
writeChanges = (0, _helpers.insertDefaultMessageProperty)({
path,

@@ -82,8 +84,11 @@ defaultMessages

});
const output = (0, _generator.default)(ast, {
retainLines: true,
retainFunctionParens: true
});
await _fs.promises.writeFile(targetFile, output.code, "utf8");
console.log(targetFile, "done");
if (writeChanges) {
const output = (0, _generator.default)(ast, {
retainLines: true,
retainFunctionParens: true
});
await _fs.promises.writeFile(targetFile, output.code, "utf8");
console.log(targetFile, "done");
}
}

@@ -90,0 +95,0 @@ };

@@ -46,5 +46,8 @@ "use strict";

firstArg.pushContainer("properties", t.objectProperty(t.identifier("defaultMessage"), t.stringLiteral(defaultMessage)));
return true;
}
return false;
};
exports.insertDefaultMessageProperty = insertDefaultMessageProperty;

@@ -10,4 +10,8 @@ #!/usr/bin/env node

const {
version
} = require("../package.json");
const program = new Command();
program.version("0.0.1");
program.version(version);
program.option("-s, --source <file>", ".json file with the default messages").option("-t, --target <glob>", "glob pattern for target files");

@@ -14,0 +18,0 @@ program.parse(process.argv);

{
"name": "intl-msgs-defaults",
"version": "0.0.1",
"version": "0.0.2",
"description": "",

@@ -5,0 +5,0 @@ "bin": "bin/index.js",

@@ -1,1 +0,74 @@

# intl-msgs-defaults
# intl-msgs-defaults
Automatically applies `defaultMessage` prop and property for `formatMessage({ id: "...", })` and `<FormattedMessage id="..." />` calls from a source file.
Consider this source code:
```js
// someModule.js
import { FormattedMessage, intl } from "react-intl";
export const TestComponent = () => {
return <FormattedMessage id="test.string" />;
};
export const TestFunc = () => {
return intl.formatMessage({
id: "test.string",
});
};
```
```js
// source.json
{
"test.string": "I like being inserted!"
}
```
Running `intl-msgs-defaults -s "./source.json" -t "someModule.js"` will update `someModule.js` to the following:
```js
// someModule.js
import { FormattedMessage, intl } from "react-intl";
export const TestComponent = () => {
return (
<FormattedMessage
id="test.string"
defaultMessage="I like being inserted!"
/>
);
};
export const TestFunc = () => {
return intl.formatMessage({
id: "test.string",
defaultMessage: "I like being inserted!",
});
};
```
# Installation
```js
npm install --save-dev intl-msgs-defaults
```
# Usage
```
Usage: intl-msgs-defaults [options]
Options:
-V, --version output the version number
-s, --source <file> .json file with the default messages
-t, --target <glob> glob pattern for target files
-h, --help display help for command
```
# Behaviour
- When there already is a `defaultMessage` the message will not be overwritten
- When no string for the ID is found in the source strings file, the string `@TODO` will be added

@@ -31,5 +31,7 @@ import fg from "fast-glob";

sourceType: "module",
plugins: ["jsx"],
plugins: ["jsx", "classProperties"],
});
let writeChanges = false;
traverse(ast, {

@@ -55,2 +57,4 @@ JSXOpeningElement: (path) => {

);
writeChanges = true;
}

@@ -64,3 +68,6 @@ },

if (isFormatMessageCall({ callee: path.node.callee })) {
insertDefaultMessageProperty({ path, defaultMessages });
writeChanges = insertDefaultMessageProperty({
path,
defaultMessages,
});
}

@@ -70,11 +77,13 @@ },

const output = generate(ast, {
retainLines: true,
retainFunctionParens: true,
});
if (writeChanges) {
const output = generate(ast, {
retainLines: true,
retainFunctionParens: true,
});
await fs.writeFile(targetFile, output.code, "utf8");
await fs.writeFile(targetFile, output.code, "utf8");
console.log(targetFile, "done");
console.log(targetFile, "done");
}
}
};

@@ -43,3 +43,7 @@ import * as t from "@babel/types";

);
return true;
}
return false;
};

@@ -5,5 +5,6 @@ #!/usr/bin/env node

const apply = require("./apply");
const { version } = require("../package.json");
const program = new Command();
program.version("0.0.1");
program.version(version);

@@ -10,0 +11,0 @@ program